Class: Trader::BitfinexBackend

Inherits:
BaseBackend show all
Defined in:
lib/trade-o-matic/adapters/bitfinex_backend.rb

Defined Under Namespace

Classes: BalanceAdaptor, OrderAdaptor

Constant Summary collapse

BASE_CUR =
Currency.for_code(:BTC)
QUOTE_CUR =
Currency.for_code(:USD)
MAIN_MARKET =
CurrencyPair.new BASE_CUR, QUOTE_CUR
CUR_EQUIV =
{
  'usd' => QUOTE_CUR,
  'btc' => BASE_CUR
}

Instance Attribute Summary

Attributes inherited from BaseBackend

#name

Instance Method Summary collapse

Methods inherited from BaseBackend

#generate_endpoint, #not_supported

Constructor Details

#initializeBitfinexBackend

Returns a new instance of BitfinexBackend.



41
42
43
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 41

def initialize
  super :bitfinex
end

Instance Method Details

#cancel_order(_session, _id) ⇒ Object



108
109
110
111
112
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 108

def cancel_order(_session, _id)
  order = wrap_order auth_post(_session, 'order/cancel', { order_id: _id })
  order = fetch_order(_session, _id) while order.status == AccountOrder::OPEN
  order
end

#create_order(_session, _pair, _volume, _price, _type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 85

def create_order(_session, _pair, _volume, _price, _type)
  params = {
    exchange: "bitfinex",
    symbol: "BTCUSD",
    side: _type == Order::BID ? 'buy' : 'sell',
    amount: _volume.to_s('F')
  }

  if _price.nil?
    params[:price] = _type == Order::BID ? '1.0' : '1000000.0' # some safe values just in case
    params[:type] = 'exchange market'
  else
    params[:price] = _price.to_s('F')
    params[:type] = 'exchange limit'
  end

  wrap_order auth_post(_session, 'order/new', params)
end

#fetch_order(_session, _id) ⇒ Object



104
105
106
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 104

def fetch_order(_session, _id)
  wrap_order auth_post(_session, 'order/status', { order_id: _id })
end

#fill_book(_book) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 49

def fill_book(_book)
  # TODO: consider book pair

  _book.prepare Time.now

  ob = public_get('book/BTCUSD')
  ob['bids'].each { |o| _book.add_bid(o['price'], o['amount']) }
  ob['asks'].each { |o| _book.add_ask(o['price'], o['amount']) }

  tx = public_get('trades/BTCUSD', { timestamp: one_hour_ago })
  tx.each do |t|
    _book.add_transaction t['price'], t['amount'], Time.at(t['timestamp'])
  end
end

#get_available_marketsObject



45
46
47
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 45

def get_available_markets
  [MAIN_MARKET]
end

#get_balance(_session, _currency) ⇒ Object



69
70
71
72
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 69

def get_balance(_session, _currency)
  raw = auth_post(_session, 'balances')
  wrap_balance raw, _currency
end

#get_orders(_session, _pair) ⇒ Object



79
80
81
82
83
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 79

def get_orders(_session, _pair)
  raw = auth_post(_session, 'orders')
  raw = raw.select { |o| o['type'].include? 'limit' }
  raw.map { |o| wrap_order o }
end

#get_session(_cred) ⇒ Object

Raises:



64
65
66
67
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 64

def get_session(_cred)
  raise AssertError, 'missing credentials' if !_cred.key?(:api_key) || !_cred.key?(:secret)
  _cred
end

#listen_transactions(_pair, &_block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 114

def listen_transactions(_pair, &_block)
  WebSocket::Client::Simple.connect('wss://api2.bitfinex.com:3000/ws') do |ws|
    ws.on :open do |event|
      ws.send(JSON.dump({ event: 'subscribe', channel: 'trades', pair: 'BTCUSD' }))
    end

    ws.on :message do |event|
      message = JSON.load(event.data)
      next if message.is_a? Hash

      if message[1] == 'te'
        _block.call(
          pair: _pair,
          ts: Time.at(message[3]),
          direction: message[5] < 0 ? Transaction::SELL : Transaction::BUY,
          price: message[4],
          volume: message[5].abs
        )
      end
    end
  end
end

#withdraw_to_endpoint(_session, _currency, _amount, _endpoint) ⇒ Object



74
75
76
77
# File 'lib/trade-o-matic/adapters/bitfinex_backend.rb', line 74

def withdraw_to_endpoint(_session, _currency, _amount, _endpoint)
  not_supported "usd withdrawals" if _currency == QUOTE_CUR
  execute_btc_withdraw(_session, _amount, _endpoint)
end