Class: Deamons::WashTrade

Inherits:
Object
  • Object
show all
Defined in:
lib/Deamons/wash_trade.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(api, market, freq) ⇒ WashTrade

Returns a new instance of WashTrade.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/Deamons/wash_trade.rb', line 8

def initialize(api, market, freq)
  @g_count = 1
  @range = 1.015
  @api = api
  @market = market
  @freq = freq
  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

Instance Method Details

#aug_pricefloat

Returns augmented price.

Returns:

  • (float)

    augmented price



44
45
46
47
48
49
50
51
# File 'lib/Deamons/wash_trade.rb', line 44

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) ⇒ TrueClass

Send stdout and stderr to log files for the child process

Parameters:

  • outfile (String)
  • errfile (String)

Returns:

  • (TrueClass)


70
71
72
73
74
75
76
77
# File 'lib/Deamons/wash_trade.rb', line 70

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

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/Deamons/wash_trade.rb', line 24

def start
  count = 0
  puts "--------#{@market} -------"
  loop do
    vol = rand(0.05...0.225) * (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, 'sell')
    @api.post_order(@market, price, vol, 'buy')
    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) ⇒ Integer and File

Attempts to write the pid of the forked process to the pid file.

Parameters:

  • pid (Integer)
  • pidfile (String)

Returns:

  • (Integer and File)


57
58
59
60
61
62
63
64
# File 'lib/Deamons/wash_trade.rb', line 57

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