Class: BitexBot::Robot

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

Overview

Documentation here! rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log(level, message) ⇒ Object



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

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.



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

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
39
# File 'lib/bitex_bot/robot.rb', line 35

def self.setup
  Bitex.api_key = Settings.maker_settings.api_key
  Bitex.sandbox = Settings.maker_settings.sandbox
  self.taker = Settings.taker_class.tap { |klass| klass.setup(Settings.taker_settings) }
end

.sleep_for(seconds) ⇒ Object



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

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

.start_robotObject



74
75
76
77
78
# File 'lib/bitex_bot/robot.rb', line 74

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

.with_cooldownObject



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

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)


106
107
108
# File 'lib/bitex_bot/robot.rb', line 106

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

#active_opening_flows?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/bitex_bot/robot.rb', line 110

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

#storeObject

The trader has a Store



115
116
117
# File 'lib/bitex_bot/robot.rb', line 115

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

#trade!Object

rubocop:disable Metrics/AbcSize



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

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.message}:\n\n#{e.backtrace.join("\n")}")
  sleep_for(60 * 3)
rescue Curl::Err::TimeoutError => e
  log(:error, "#{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