Class: Irix::Huobi
- Inherits:
-
Peatio::Upstream::Base
- Object
- Peatio::Upstream::Base
- Irix::Huobi
- 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
-
#asks ⇒ Object
Returns the value of attribute asks.
-
#bids ⇒ Object
Returns the value of attribute bids.
-
#increment_count ⇒ Object
Returns the value of attribute increment_count.
-
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
-
#snap ⇒ Object
Returns the value of attribute snap.
-
#snapshot_time ⇒ Object
Returns the value of attribute snapshot_time.
Instance Method Summary collapse
- #detect_order(msg) ⇒ Object
- #detect_trade(msg) ⇒ Object
- #fill_increment(inc) ⇒ Object
- #fill_side(inc, side) ⇒ Object
-
#initialize(config) ⇒ Huobi
constructor
WS huobi global websocket: “wss://api.huobi.pro/ws/” WS for krw markets websocket: “wss://api-cloud.huobi.co.kr/ws/”.
- #publish_increment ⇒ Object
- #publish_snapshot ⇒ Object
- #subscribe_orderbook(market, ws) ⇒ Object
- #subscribe_trades(market, ws) ⇒ Object
- #ws_connect ⇒ Object
- #ws_read_message(msg) ⇒ Object
- #ws_read_public_message(msg) ⇒ Object
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
#asks ⇒ Object
Returns the value of attribute asks.
11 12 13 |
# File 'lib/irix/huobi.rb', line 11 def asks @asks end |
#bids ⇒ Object
Returns the value of attribute bids.
11 12 13 |
# File 'lib/irix/huobi.rb', line 11 def bids @bids end |
#increment_count ⇒ Object
Returns the value of attribute increment_count.
11 12 13 |
# File 'lib/irix/huobi.rb', line 11 def increment_count @increment_count end |
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
11 12 13 |
# File 'lib/irix/huobi.rb', line 11 def sequence_number @sequence_number end |
#snap ⇒ Object
Returns the value of attribute snap.
11 12 13 |
# File 'lib/irix/huobi.rb', line 11 def snap @snap end |
#snapshot_time ⇒ Object
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_increment ⇒ Object
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_snapshot ⇒ Object
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_connect ⇒ Object
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 (msg) data = Zlib::GzipReader.new(StringIO.new(msg.data.map(&:chr).join)).read Rails.logger.debug { "received websocket message: #{data}" } object = JSON.parse(data) (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 (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 |