Class: Deamons::SimpleMm

Inherits:
Object
  • Object
show all
Defined in:
lib/Deamons/simple_mm.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) ⇒ SimpleMm

Returns a new instance of SimpleMm.



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

def initialize(api, market)
  @order_book = { bids: [], asks: [] }
  @api = api
  @market = market
  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"
end

Instance Method Details

#populate_order_book(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/Deamons/simple_mm.rb', line 37

def populate_order_book(data)
  range = 499
  nums = (0..range).to_a.shuffle
  for i in (0..range)
    ord = data[:bids][nums[i]]
    txID = JSON.parse(@api.post_order(@market, ord[0].to_f, ord[1].to_f / 10, 'buy'))
    puts txID
    ord.push(txID['id'])
    @api.cancel_order(@order_book[:bids][nums[i]][-1]) if @order_book[:bids].size > 400
    ord = data[:asks][nums[i]]
    txID = JSON.parse(@api.post_order(@market, ord[0].to_f, ord[1].to_f / 10, 'sell'))
    ord.push(txID['id'])
    @api.cancel_order(@order_book[:asks][nums[i]][-1]) if @order_book[:asks].size > 400
    sleep(1)
  end
  @order_book = data
  puts data.inspect
end

#redirect(outfile, errfile) ⇒ TrueClass

Send stdout and stderr to log files for the child process

Parameters:

  • outfile (String)
  • errfile (String)

Returns:

  • (TrueClass)


73
74
75
76
77
78
79
80
# File 'lib/Deamons/simple_mm.rb', line 73

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/Deamons/simple_mm.rb', line 22

def start
  count = 0
  puts "--------#{@market} -------"
  loop do
    data = Utils.binane_order_book
    populate_order_book(data)
    sleep(500)
    count += 1
  rescue ::StandardError => e
    STDERR.puts e.backtrace
    STDERR.puts '\n'
    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)


60
61
62
63
64
65
66
67
# File 'lib/Deamons/simple_mm.rb', line 60

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