Class: Deamons::WashTrade
- Inherits:
-
Object
- Object
- Deamons::WashTrade
- Defined in:
- lib/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
-
#initialize(api, market, freq) ⇒ WashTrade
constructor
A new instance of WashTrade.
- #move_price ⇒ Object
-
#redirect(outfile, errfile) ⇒ Object
Send stdout and stderr to log files for the child process.
- #start ⇒ Object
-
#write_pid_file(pid, pidfile) ⇒ Object
Attempts to write the pid of the forked process to the pid file.
Constructor Details
#initialize(api, market, freq) ⇒ WashTrade
Returns a new instance of WashTrade.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/wash_trade.rb', line 6 def initialize(api, market, freq) @g_count = 1 @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
#move_price ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/wash_trade.rb', line 37 def move_price range = 1.015 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) ⇒ Object
Send stdout and stderr to log files for the child process
58 59 60 61 62 63 64 65 |
# File 'lib/wash_trade.rb', line 58 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 ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/wash_trade.rb', line 21 def start count = 0 loop do vol = rand(0.2...3) price = move_price @api.post_order(@market, price, vol, 'sell') delay = (1.00 / @freq) sleep(delay) @api.post_order(@market, price, vol, 'buy') puts "order ##{count} placed" count += 1 end rescue ::StandardError => e puts e end |
#write_pid_file(pid, pidfile) ⇒ Object
Attempts to write the pid of the forked process to the pid file.
48 49 50 51 52 53 54 55 |
# File 'lib/wash_trade.rb', line 48 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 |