Class: Cryptoexchange::Exchanges::Bitfinex::Services::TradeStream

Inherits:
Services::TradeStream show all
Defined in:
lib/cryptoexchange/exchanges/bitfinex/services/trade_stream.rb

Instance Method Summary collapse

Instance Method Details

#parse_message(message, market_pair) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cryptoexchange/exchanges/bitfinex/services/trade_stream.rb', line 21

def parse_message(message, market_pair)
  # [ 5, 'te', '1234-BTCUSD', 1443659698, 236.42, 0.49064538 ]
  # [ 5, 'tu', '1234-BTCUSD', 15254529, 1443659698, 236.42, 0.49064538 ]
  # [67452,6614.9,36.37426215,6615,71.7470363,42.2,0.0064,6614.9,9981.07371415,6741.4366438,6570]
  #
  # CHANNEL_ID integer	Channel ID
  # SEQ	       string	  Trade sequence id
  # ID	       int	    Trade database id
  # TIMESTAMP	 int	    Unix timestamp of the trade.
  # PRICE	     float    Price at which the trade was executed
  # ±AMOUNT	   float    How much was bought (positive) or sold (negative)
  trade           = Cryptoexchange::Models::Trade.new
  trade.base      = market_pair.base
  trade.target    = market_pair.target
  trade.market    = Bitfinex::Market::NAME
  trade.trade_id  = message[3]
  trade.type      = message[6] > 0 ? 'buy' : 'sell' # The order that causes the trade determines if it is a buy or a sell.
  trade.price     = message[5]
  trade.amount    = message[6]
  trade.timestamp = message[4]
  trade.payload   = message

  trade
end

#subscribe_event(market_pair) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/cryptoexchange/exchanges/bitfinex/services/trade_stream.rb', line 9

def subscribe_event(market_pair)
  {
    event:   "subscribe",
    channel: "trades",
    pair:    "#{market_pair.base}#{market_pair.target}"
  }.to_json
end

#urlObject



5
6
7
# File 'lib/cryptoexchange/exchanges/bitfinex/services/trade_stream.rb', line 5

def url
  Cryptoexchange::Exchanges::Bitfinex::Market::WS_URL
end

#valid_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/cryptoexchange/exchanges/bitfinex/services/trade_stream.rb', line 17

def valid_message?(message)
  message.is_a?(Array) && message.length == 7
end