Class: BitexBot::Robot

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bitex_bot/robot.rb

Overview

Documentation here!

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log(level, message) ⇒ Object



59
60
61
# File 'lib/bitex_bot/robot.rb', line 59

def self.log(level, message)
  logger.send(level, message)
end

.run!Object

Trade constantly respecting cooldown times so that we don’t get banned by api clients.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bitex_bot/robot.rb', line 41

def self.run!
  bot = start_robot
  self.cooldown_until = Time.now
  loop do
    start_time = Time.now
    next if start_time < cooldown_until

    self.current_cooldowns = 0
    bot.trade!
    self.cooldown_until = start_time + current_cooldowns.seconds
  end
end

.setupObject



35
36
37
38
# File 'lib/bitex_bot/robot.rb', line 35

def self.setup
  self.maker = Settings.maker_class.new(Settings.maker_settings)
  self.taker = Settings.taker_class.new(Settings.taker_settings)
end

.sleep_for(seconds) ⇒ Object



54
55
56
# File 'lib/bitex_bot/robot.rb', line 54

def self.sleep_for(seconds)
  sleep(seconds)
end

.start_robotObject



72
73
74
75
76
# File 'lib/bitex_bot/robot.rb', line 72

def self.start_robot
  setup
  log(:info, "Loading trading robot, ctrl+c *once* to exit gracefully.\n")
  new
end

.with_cooldownObject



64
65
66
67
68
69
# File 'lib/bitex_bot/robot.rb', line 64

def self.with_cooldown
  yield.tap do
    self.current_cooldowns += 1
    sleep_for(0.1)
  end
end

Instance Method Details

#active_closing_flows?Boolean

rubocop:enable Metrics/AbcSize

Returns:

  • (Boolean)


104
105
106
# File 'lib/bitex_bot/robot.rb', line 104

def active_closing_flows?
  [BuyClosingFlow, SellClosingFlow].map(&:active).any?(&:exists?)
end

#active_opening_flows?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/bitex_bot/robot.rb', line 108

def active_opening_flows?
  [BuyOpeningFlow, SellOpeningFlow].map(&:active).any?(&:exists?)
end

#storeObject

The trader has a Store



113
114
115
# File 'lib/bitex_bot/robot.rb', line 113

def store
  @store ||= Store.first || Store.create
end

#trade!Object

rubocop:disable Metrics/AbcSize



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bitex_bot/robot.rb', line 79

def trade!
  sync_opening_flows if active_opening_flows?
  finalise_some_opening_flows
  shutdown! if shutdable?
  start_closing_flows if open_positions?
  sync_closing_flows if active_closing_flows?
  start_opening_flows_if_needed
rescue CannotCreateFlow => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
  sleep_for(60 * 3)
rescue Curl::Err::TimeoutError => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
  sleep_for(15)
rescue OrderNotFound => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
rescue ApiWrapperError => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
rescue OrderArgumentError => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
rescue StandardError => e
  notify("#{e.class} - #{e.message}\n\n#{e.backtrace.join("\n")}")
  sleep_for(60 * 2)
end