class Livermore::Bot

Constants

CONFIG_PATH

Attributes

ask[RW]
base_fund[R]
best_ask[R]
best_bid[R]
bid[RW]
market[R]
open_orders[RW]
orders_to_cancel[RW]
orders_to_create[RW]
quote_fund[R]

Public Class Methods

new(config_path = CONFIG_PATH) click to toggle source
# File lib/livermore/bot.rb, line 8
def initialize(config_path = CONFIG_PATH)
  config = load_config(config_path)

  initialize_exchange_client(config)
  initialize_instance_variables(config)
  initialize_strategy(config)
end

Public Instance Methods

cancel_all() click to toggle source
# File lib/livermore/bot.rb, line 51
def cancel_all
  fetch_orders
  @orders_to_cancel = open_orders
  cancel_orders
end
cancel_orders() click to toggle source
# File lib/livermore/bot.rb, line 33
def cancel_orders
  orders_to_cancel.each do |order|
    response = cancel_order(order.id)

    update_fund(:+, order) if response.code == 200
  end
end
create_orders() click to toggle source
# File lib/livermore/bot.rb, line 41
def create_orders
  orders_to_create.each do |order|
    response = create_order(@order_klass.new(order).as_json)

    update_fund(:-, order) if response.code == 201
  end

  orders_to_create.clear
end
fetch_orders() click to toggle source
# File lib/livermore/bot.rb, line 27
def fetch_orders
  response = my_orders(market: @market)

  @open_orders = response.open_orders
end
fetch_top_tier() click to toggle source
# File lib/livermore/bot.rb, line 20
def fetch_top_tier
  response = orderbook(@market)

  @best_ask = response.best_ask
  @best_bid = response.best_bid
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/livermore/bot.rb, line 66
def method_missing(method, *args)
  if @client.respond_to?(method)
    @response_klass.new(@client.send(method, *args))
  else
    super
  end
end
trade() click to toggle source
# File lib/livermore/bot.rb, line 16
def trade
  @strategy.trade
end
update_fund(method, order) click to toggle source
# File lib/livermore/bot.rb, line 57
def update_fund(method, order)
  case order.type
  when Order::ASK
    @base_fund = @base_fund.send(method, order.amount)
  when Order::BID
    @quote_fund = @quote_fund.send(method, order.amount * order.price)
  end
end

Private Instance Methods

initialize_exchange_client(config) click to toggle source
# File lib/livermore/bot.rb, line 82
def initialize_exchange_client(config)
  name = config[:exchange][:name]
  key = config[:exchange][:key]
  secret = config[:exchange][:secret]

  @client = CLIENTS[name][:client].new(key, secret)
  @response_klass = CLIENTS[name][:response]
  @order_klass = CLIENTS[name][:order]
end
initialize_instance_variables(config) click to toggle source
# File lib/livermore/bot.rb, line 92
def initialize_instance_variables(config)
  @market = config[:market]
  @base_fund = config[:base_fund]
  @quote_fund = config[:quote_fund]
  @orders_to_create = []
end
initialize_strategy(config) click to toggle source
# File lib/livermore/bot.rb, line 99
def initialize_strategy(config)
  name = config[:strategy][:name]
  path = config[:strategy][:path]

  @strategy = STRATEGIES[name].new(self, path)
end
load_config(path) click to toggle source
# File lib/livermore/bot.rb, line 76
def load_config(path)
  template = ERB.new(File.read(path))

  YAML.load(template.result)
end