Class: Trader::Account

Inherits:
Object
  • Object
show all
Includes:
MarketLoader
Defined in:
lib/trade-o-matic/core/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MarketLoader

#available_markets, #has_market_for?, #market_for

Constructor Details

#initialize(_backend, _session) ⇒ Account

Returns a new instance of Account.



7
8
9
10
# File 'lib/trade-o-matic/core/account.rb', line 7

def initialize(_backend, _session)
  @backend = _backend
  @session = _session
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



5
6
7
# File 'lib/trade-o-matic/core/account.rb', line 5

def backend
  @backend
end

Instance Method Details

#balance_for(_currency) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/trade-o-matic/core/account.rb', line 29

def balance_for(_currency)
  _currency = Currency.for_code _currency
  ensure_supported_currency _currency

  # IDEA: cache balances by currency code.
  balance = Balance.new backend, session, _currency
  balance.refresh!
  balance
end

#create_order(_order) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trade-o-matic/core/account.rb', line 39

def create_order(_order)
  ensure_supported_market _order.pair
  raw_order = backend.create_order(
    session,
    _order.pair,
    _order.volume.amount,
    _order.limit? ? _order.price.amount : nil,
    _order.instruction
  )

  AccountOrder.new backend, session, raw_order
end

#find_order(_id) ⇒ Object



52
53
54
55
56
# File 'lib/trade-o-matic/core/account.rb', line 52

def find_order(_id)
  raw_order = backend.fetch_order session, _id
  # TODO: maybe Position.new(backend, session, Market.new(backend, raw_order.pair), raw_order)
  AccountOrder.new backend, session, raw_order
end

#list_ordersObject



58
59
60
# File 'lib/trade-o-matic/core/account.rb', line 58

def list_orders
  # TODO.
end

#using(_pair, _quote = nil, &_block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trade-o-matic/core/account.rb', line 12

def using(_pair, _quote=nil, &_block)
  _pair = CurrencyPair.for_code _pair, _quote

  proxy_market = find_exact_market _pair
  proxy_market = find_compatible_market _pair if proxy_market.nil?
  backend.not_supported _pair if proxy_market.nil?

  proxy = AccountProxy.new self, proxy_market, _pair

  return proxy if _block.nil?
  _block.call proxy
end

#using_default_pair(&_block) ⇒ Object



25
26
27
# File 'lib/trade-o-matic/core/account.rb', line 25

def using_default_pair(&_block)
  using available_markets.first, &_block
end