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.



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

def initialize
  super :bitfinex
end

Instance Method Details

#cancel_order(_session, _id) ⇒ Object



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

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



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

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



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

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

#fill_book(_book) ⇒ Object



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

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



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

def get_available_markets
  [MAIN_MARKET]
end

#get_balance(_session, _currency) ⇒ Object



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

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

#get_orders(_session, _pair) ⇒ Object



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

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:



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

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



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

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