class Trader::GameBackend

This is needed to prevent one of the subclass files from defining the class without the superclass.

Constants

STATUS_MAP

Attributes

state[R]
verbose[R]

Public Class Methods

new() click to toggle source
Calls superclass method Trader::BaseBackend::new
# File lib/trade-o-matic/adapters/game_backend.rb, line 46
def initialize
  super :game
  @state = State.new
end

Public Instance Methods

cancel_order(_session, _order_id) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 128
def cancel_order(_session, _order_id)
  with_session do
    cancel_order = CancelOrder.new
    cancel_order.state = state
    cancel_order.account = _session
    cancel_order.order_id = _order_id.to_i
    cancel_order.perform
  end
end
create_order(_session, _pair, _volume, _price, _instruction) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 115
def create_order(_session, _pair, _volume, _price, _instruction)
  with_session do
    execute_order = ExecuteOrder.new
    execute_order.state = state
    execute_order.account = _session
    execute_order.pair = _pair
    execute_order.volume = SFM.encode _volume
    execute_order.limit = SFM.encode _price
    execute_order.instruction = _instruction
    execute_order.perform
  end
end
decrement_balance(_account_name, _currency, _amount = nil) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 63
def decrement_balance(_account_name, _currency, _amount = nil)
  if _amount.nil?
    _amount = _currency.amount
    _currency = _currency.currency
  end

  with_session do
    balance = state.balance_for(_account_name, Currency.for_code(_currency))
    balance['available'] -= SFM.encode(_amount)
    balance['available'] = 0 if balance['available'] < 0
  end
end
fetch_order(_session, _order_id) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 109
def fetch_order(_session, _order_id)
  with_session(true) do
    GameOrder.new state.find_order_by_id(_order_id.to_i)[:order]
  end
end
fill_book(_book) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 84
def fill_book(_book)
  # TODO
end
get_available_markets() click to toggle source

backend implementation:

# File lib/trade-o-matic/adapters/game_backend.rb, line 78
def get_available_markets
  with_session(true) do
    state.all_markets
  end
end
get_balance(_session, _currency) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 93
def get_balance(_session, _currency)
  with_session(true) do
    balance = state.balance_for(_session, _currency)
    GameBalance.new balance
  end
end
get_orders(_session, _pair) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 100
def get_orders(_session, _pair)
  with_session(true) do
    market = state.market_for(_pair)
    open = market['open'].select { |o| o['account'] = _session }
    closed = market['closed'].select { |o| o['account'] = _session }
    (open + closed).map { |o| GameOrder.new o }
  end
end
get_session(_config) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 88
def get_session(_config)
  raise AssertError, 'must provide login information' if _config.nil? or _config[:name].nil?
  _config[:name]
end
increment_balance(_account_name, _currency, _amount = nil) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 51
def increment_balance(_account_name, _currency, _amount = nil)
  if _amount.nil?
    _amount = _currency.amount
    _currency = _currency.currency
  end

  with_session do
    balance = state.balance_for(_account_name, Currency.for_code(_currency))
    balance['available'] += SFM.encode(_amount)
  end
end

Private Instance Methods

config() click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 165
def config
  Configuration
end
ensure_required_markets() click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 160
def ensure_required_markets
  return false unless config.markets
  config.markets.each { |mc| state.market_for(mc) }
end
info(_msg, _color = :white) click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 169
def info(_msg, _color = :white)
  puts "GameEx: #{_msg}".color(_color) if verbose
end
restore_state_from_file() click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 152
def restore_state_from_file
  state.load_from_file config.db_file
end
save_state_to_file() click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 156
def save_state_to_file
  state.store_to_file config.db_file
end
use_state_file?() click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 148
def use_state_file?
  !config.db_file.nil?
end
with_session(_idemponent = false) { || ... } click to toggle source
# File lib/trade-o-matic/adapters/game_backend.rb, line 140
def with_session(_idemponent = false)
  restore_state_from_file if use_state_file?
  ensure_required_markets
  result = yield
  save_state_to_file if !_idemponent && use_state_file?
  return result
end