class Trader::GameBackend::ExecuteOrder

Public Instance Methods

perform() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 4
def perform
  @market = state.market_for pair
  @base_balance = state.balance_for account, @market['base']
  @quote_balance = state.balance_for account, @market['quote']

  if bid?
    raise 'Not enough funds' if limit_order? and !enough_funds_for_bid?
    load_bid_candidates
    match_candidates
    raise 'Not enough funds' if market_order? and !can_execute_market_bid?
    execute_bid_txs
    update_balance_on_bid unless order_fully_consumed?
    store_order 'bid'
  else
    raise 'Not enough funds' unless enough_funds_for_ask?
    load_ask_candidates
    match_candidates
    execute_ask_txs
    update_balance_on_ask unless order_fully_consumed?
    store_order 'ask'
  end
end

Private Instance Methods

bid?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 29
def bid?
  instruction == Order::BID
end
can_execute_market_bid?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 92
def can_execute_market_bid?
  available = @quote_balance['available']
  @txs.each do |tx|
    quote = safe_quote(tx[:volume], tx[:order]['limit'])
    return false if quote > available
    available -= quote
  end
  true
end
enough_funds_for_ask?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 45
def enough_funds_for_ask?
  @base_balance['available'] >= volume
end
enough_funds_for_bid?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 41
def enough_funds_for_bid?
  @quote_balance['available'] >= safe_quote(volume, limit)
end
execute_ask_txs() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 111
def execute_ask_txs
  process_each_tx do |tx|
    @base_balance['available'] -= tx[:volume]
    @quote_balance['available'] += tx_quote(tx)
    order_base_balance(tx[:order])['available'] += tx[:volume]
    order_quote_balance(tx[:order])['frozen'] -= tx_quote(tx)
  end
end
execute_bid_txs() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 102
def execute_bid_txs
  process_each_tx do |tx|
    @base_balance['available'] -= tx[:volume]
    @quote_balance['available'] += tx_quote(tx)
    order_base_balance(tx[:order])['available'] += tx[:volume]
    order_quote_balance(tx[:order])['frozen'] -= tx_quote(tx)
  end
end
limit_order?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 37
def limit_order?
  !limit.nil?
end
load_ask_candidates() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 63
def load_ask_candidates
  @candidates = @market['open'].select do |order|
    next false if order['instruction'] != 'bid'
    next true if limit.nil?
    order['limit'] >= limit
  end

  @candidates.sort! do |a, b|
    r = b['limit'] <=> a['limit']
    r = a['ts'] <=> b['ts'] if r == 0
    r
  end
end
load_bid_candidates() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 49
def load_bid_candidates
  @candidates = @market['open'].select do |order|
    next false if order['instruction'] != 'ask'
    next true if limit.nil?
    order['limit'] <= limit
  end

  @candidates.sort! do |a, b|
    r = a['limit'] <=> b['limit']
    r = a['ts'] <=> b['ts'] if r == 0
    r
  end
end
market_order?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 33
def market_order?
  limit.nil?
end
match_candidates() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 77
def match_candidates
  @txs = []
  @remaining = volume
  @candidates.each do |other|
    if @remaining >= other['volume']
      @remaining -= other['volume']
      @txs << { order: other, volume: other['volume'] }
    else
      @txs << { order: other, volume: @remaining }
      @remaining = 0.0
      break
    end
  end
end
order_base_balance(_order) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 165
def order_base_balance(_order)
  state.balance_for _order['account'], @market['base']
end
order_fully_consumed?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 153
def order_fully_consumed?
  market_order? or @remaining <= 0
end
order_quote_balance(_order) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 169
def order_quote_balance(_order)
  state.balance_for _order['account'], @market['quote']
end
process_each_tx() { |tx| ... } click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 157
def process_each_tx
  @txs.each do |tx|
    yield tx
    tx[:order]['volume'] -= tx[:volume]
    update_status tx[:order]
  end
end
safe_quote(_volume, _price) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 185
def safe_quote(_volume, _price)
  SFM.quote(_volume, _price)
end
store_order(_instruction) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 120
def store_order(_instruction)
  order = {
    'id' => state.unique_id,
    'account' => account,
    'pair' => pair.to_s, # just a little denorm to simplify
    'instruction' => _instruction,
    'limit' => limit,
    'volume' => @remaining,
    'original_volume' => volume
  }

  if order_fully_consumed?
    order['status'] = 'closed'
    @market['closed'] << order
  else
    order['status'] = 'open'
    @market['open'] << order
  end

  GameOrder.new order
end
tx_quote(_tx) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 173
def tx_quote(_tx)
  safe_quote(_tx[:volume], _tx[:order]['limit'])
end
update_balance_on_ask() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 148
def update_balance_on_ask
  @base_balance['available'] -= @remaining
  @base_balance['frozen'] += @remaining
end
update_balance_on_bid() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 142
def update_balance_on_bid
  remaining_quote = safe_quote(@remaining, limit)
  @quote_balance['available'] -= remaining_quote
  @quote_balance['frozen'] += remaining_quote
end
update_status(_order) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/execute_order.rb, line 177
def update_status(_order)
  if _order['volume'] == 0
    _order['status'] = 'closed'
    @market['open'].delete _order
    @market['closed'] << _order
  end
end