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.



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

def initialize
  super :bitfinex
end

Instance Method Details

#cancel_order(_session, _id) ⇒ Object



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

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



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

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



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

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

#fill_book(_book) ⇒ Object



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

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



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

def get_available_markets
  [MAIN_MARKET]
end

#get_balance(_session, _currency) ⇒ Object



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

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

#get_orders(_session, _pair) ⇒ Object



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

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:



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

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

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



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

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