Class: Trader::BitstampBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/trade-o-matic/adapters/bitstamp_backend.rb

Defined Under Namespace

Classes: BackendBalance, BitstampOrder

Constant Summary collapse

BASE_CUR =
Currency.for_code(:BTC)
QUOTE_CUR =
Currency.for_code(:BITSTAMP_USD)
MAIN_MARKET =
CurrencyPair.new BASE_CUR, QUOTE_CUR
TYPE_MAP =
{
  0 => Order::BID,
  1 => Order::ASK
}

Instance Method Summary collapse

Constructor Details

#initialize(_session = nil) ⇒ BitstampBackend

Returns a new instance of BitstampBackend.



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

def initialize(_session=nil)
  @session = _session
end

Instance Method Details

#cancel_order(_session, _id) ⇒ Object



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

def cancel_order(_session, _id)
  # TODO
end

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



91
92
93
94
95
96
97
98
99
100
# File 'lib/trade-o-matic/adapters/bitstamp_backend.rb', line 91

def create_order(_session, _pair, _volume, _price, _type)
  raise 'market not supported' unless _pair == MAIN_MARKET
  raise 'market orders not supported' if _price.nil?

  normalize_raw_order execute_request(
    _session || session,
    _type == Order::BID ? 'buy' : 'sell',
    { amount: _volume, price: _price }
  )
end

#fetch_order(_session, _id) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/trade-o-matic/adapters/bitstamp_backend.rb', line 102

def fetch_order(_session, _id)
  normalize_raw_order_status _id, execute_request(
    _session || session,
    'order_status',
    { id: _id['id'] }
  )
end

#fill_book(_book) ⇒ Object



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

def fill_book(_book)
  # TODO: consider book pair

  _book.prepare Time.now

  ob = execute_request(nil, 'order_book')
  ob['bids'].each { |o| _book.add_bid(o[0].to_f, o[1].to_f) }
  ob['asks'].each { |o| _book.add_ask(o[0].to_f, o[1].to_f) }

  tx = execute_request(nil, 'transactions')
  tx.each do |t|
    _book.add_transaction t['price'].to_f, t['amount'].to_f, Time.at(t['date'].to_i)
  end
end

#get_available_marketsObject



49
50
51
# File 'lib/trade-o-matic/adapters/bitstamp_backend.rb', line 49

def get_available_markets
  [MAIN_MARKET]
end

#get_balance(_session, _currency) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/trade-o-matic/adapters/bitstamp_backend.rb', line 72

def get_balance(_session, _currency)
  raise "#{_currency} not supported" unless _currency == BASE_CUR || _currency == QUOTE_CUR

  raw = execute_request(_session || session, 'balance')

  if _currency == BASE_CUR
    return BackendBalance.new raw['btc_balance'].to_f, raw['btc_available'].to_f
  else
    return BackendBalance.new raw['usd_balance'].to_f, raw['usd_available'].to_f
  end
end

#get_orders(_session, _pair) ⇒ Object



84
85
86
87
88
89
# File 'lib/trade-o-matic/adapters/bitstamp_backend.rb', line 84

def get_orders(_session, _pair)
  raise 'market not supported' unless _pair == MAIN_MARKET

  raw_orders = execute_request(_session || session, 'open_orders')
  raw_orders.map { |o| normalize_raw_order o }
end

#get_session(_credentials) ⇒ Object



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

def get_session(_credentials)
  _credentials
end