class Deamons::Test

Simple process to add wash trades to a market in order to simulate trade matching for QA and dev

Public Class Methods

new(api, market, freq, vol) click to toggle source
# File lib/Deamons/test.rb, line 8
def initialize(api, market, freq, vol)
  @g_count = 1
  @range = 1.015
  @api = api
  @market = market
  @freq = freq
  @volume = vol
  Process.fork do
    Process.daemon(true)
    pid = Process.pid
    redirect("#{pid}.outfile", "#{pid}.errfile")
    write_pid_file(pid, "#{pid}.pid")
    start
  end
  puts "SimBot started on #{market} @ freq #{freq}."
end

Public Instance Methods

aug_price() click to toggle source

@return [float] augmented price

# File lib/Deamons/test.rb, line 45
def aug_price
  if @g_count > 60
    @range = rand(1.003...1.010)
    @g_count = 0
  end
  @g_count += 1
  Utils.quote(@market) * rand(1.002...@range)
end
redirect(outfile, errfile) click to toggle source

Send stdout and stderr to log files for the child process @param [String] outfile @param [String] errfile @return [TrueClass]

# File lib/Deamons/test.rb, line 71
def redirect(outfile, errfile)
  $stdin.reopen '/dev/null'
  out = File.new outfile, "a"
  err = File.new errfile, "a"
  $stdout.reopen out
  $stderr.reopen err
  $stdout.sync = $stderr.sync = true
end
start() click to toggle source
# File lib/Deamons/test.rb, line 25
def start
  count = 0
  puts "--------#{@market} -------"
  loop do
    vol = rand(@volume...@volume*1.3) * (Math.sin(Time.now.hour / 6.00)
                                + 1.00) * Utils.rand_for_hour(1.5, 2)
    price = aug_price
    @api.post_order(@market, price, vol, 'buy') if rand(1...100) < 22
    @api.post_order(@market, price, vol, 'sell') if rand(1...100) < 22
    puts "order ##{count} placed"
    delay = (1 / @freq).to_i
    sleep(rand(delay...delay + 2))
    count += 1
  rescue ::StandardError => e
    STDERR.puts e.backtrace
    next
  end
end
write_pid_file(pid, pidfile) click to toggle source

Attempts to write the pid of the forked process to the pid file. @param [Integer] pid @param [String] pidfile @return [Integer and File]

# File lib/Deamons/test.rb, line 58
def write_pid_file(pid, pidfile)
  File.open pidfile, "w" do |f|
    f.write pid
  end
rescue ::Exception => e
  $stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}"
  Process.kill "HUP", pid
end