Class: BitexBot::Robot

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bitex_bot/robot.rb', line 29

def self.run!
  setup
  logger.info("Loading trading robot, ctrl+c *once* to exit gracefully.")
  self.cooldown_until = Time.now
  bot = new

  while true
    start_time = Time.now
    return if start_time < cooldown_until
    self.current_cooldowns = 0
    bot.trade!
    self.cooldown_until = start_time + current_cooldowns.seconds
  end
end

.setupObject



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

def self.setup
  Bitex.api_key = Settings.bitex
  Bitstamp.setup do |config|
    config.key = Settings.bitstamp.key
    config.secret = Settings.bitstamp.secret
    config.client_id = Settings.bitstamp.client_id.to_s
  end
end

.with_cooldown(&block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/bitex_bot/robot.rb', line 53

def self.with_cooldown(&block)
  result = block.call
  return result if test_mode
  self.current_cooldowns += 1
  sleep 0.1 
  return result
end

Instance Method Details

#active_closing_flows?Boolean

Returns:

  • (Boolean)


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

def active_closing_flows?
  BuyClosingFlow.active.exists? || SellClosingFlow.active.exists?
end

#active_opening_flows?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/bitex_bot/robot.rb', line 154

def active_opening_flows?
  BuyOpeningFlow.active.exists? || SellOpeningFlow.active.exists?
end

#finalise_some_opening_flowsObject



84
85
86
87
88
89
# File 'lib/bitex_bot/robot.rb', line 84

def finalise_some_opening_flows
  [BuyOpeningFlow, SellOpeningFlow].each do |kind|
    flows = self.class.graceful_shutdown ? kind.active : kind.old_active
    flows.each{|flow| flow.finalise! }
  end
end

#notify(message) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bitex_bot/robot.rb', line 158

def notify(message)
  self.class.logger.error(message)
  if Settings.mailer
    mail = Mail.new do
      from Settings.mailer.from
      to Settings.mailer.to
      subject 'Notice from your robot trader'
      body message
    end
    mail.delivery_method(Settings.mailer.method.to_sym,
      Settings.mailer.options.symbolize_keys)
    mail.deliver!
  end
end

#open_positions?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/bitex_bot/robot.rb', line 95

def open_positions?
  OpenBuy.open.exists? || OpenSell.open.exists?
end

#start_closing_flowsObject



91
92
93
# File 'lib/bitex_bot/robot.rb', line 91

def start_closing_flows
  [BuyClosingFlow, SellClosingFlow].each{|kind| kind.close_open_positions}
end

#start_opening_flows_if_neededObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bitex_bot/robot.rb', line 114

def start_opening_flows_if_needed
  return if open_positions?
  return if active_closing_flows?
  return if self.class.graceful_shutdown
  
  
  recent_buying, recent_selling =
    [BuyOpeningFlow, SellOpeningFlow].collect do |kind|
      threshold = (Settings.time_to_live / 2).seconds.ago
      kind.active.where('created_at > ?', threshold).first
    end

  return if recent_buying && recent_selling
  
  balances = with_cooldown{ Bitstamp.balance }
  order_book = with_cooldown{ Bitstamp.order_book }
  transactions = with_cooldown{ Bitstamp.transactions }
  
  unless recent_buying
    BuyOpeningFlow.create_for_market(
      balances['btc_available'].to_d,
      order_book['bids'],
      transactions,
      Bitex::Profile.get[:fee],
      balances['fee'].to_d )
  end
  unless recent_selling
    SellOpeningFlow.create_for_market(
      balances['usd_available'].to_d,
      order_book['asks'],
      transactions,
      Bitex::Profile.get[:fee],
      balances['fee'].to_d )
  end
end

#sync_closing_flowsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/bitex_bot/robot.rb', line 99

def sync_closing_flows
  orders = with_cooldown{ Bitstamp.orders.all }
  transactions = with_cooldown{ Bitstamp.user_transactions.all }

  [BuyClosingFlow, SellClosingFlow].each do |kind|
    kind.active.each do |flow|
      flow.sync_closed_positions(orders, transactions)
    end
  end
end

#sync_opening_flowsObject



150
151
152
# File 'lib/bitex_bot/robot.rb', line 150

def sync_opening_flows
  [SellOpeningFlow, BuyOpeningFlow].each{|o| o.sync_open_positions }
end

#trade!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bitex_bot/robot.rb', line 65

def trade!
  finalise_some_opening_flows
  if(!active_opening_flows? && !open_positions? &&
    !active_closing_flows? && self.class.graceful_shutdown)
    self.class.logger.info("Shutdown completed")
    exit
  end
  sync_opening_flows if active_opening_flows?
  start_closing_flows if open_positions?
  sync_closing_flows if active_closing_flows?
  start_opening_flows_if_needed
rescue CannotCreateFlow => e
  self.notify("#{e.message}:\n\n#{e.backtrace.join("\n")}")
  BitexBot::Robot.graceful_shutdown = true
rescue StandardError => e
  self.notify("#{e.message}:\n\n#{e.backtrace.join("\n")}")
  sleep 30 unless self.class.test_mode
end

#with_cooldown(&block) ⇒ Object



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

def with_cooldown(&block)
  self.class.with_cooldown(&block)
end