Class: BitexBot::Robot
- Inherits:
-
Object
- Object
- BitexBot::Robot
- Defined in:
- lib/bitex_bot/robot.rb
Class Method Summary collapse
-
.run! ⇒ Object
Trade constantly respecting cooldown times so that we don’t get banned by api clients.
- .setup ⇒ Object
- .with_cooldown(&block) ⇒ Object
Instance Method Summary collapse
- #active_closing_flows? ⇒ Boolean
- #active_opening_flows? ⇒ Boolean
- #finalise_some_opening_flows ⇒ Object
- #notify(message) ⇒ Object
- #open_positions? ⇒ Boolean
- #start_closing_flows ⇒ Object
- #start_opening_flows_if_needed ⇒ Object
-
#store ⇒ Object
The trader has a Store.
- #sync_closing_flows ⇒ Object
- #sync_opening_flows ⇒ Object
- #trade! ⇒ Object
- #with_cooldown(&block) ⇒ Object
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 next if start_time < cooldown_until self.current_cooldowns = 0 bot.trade! self.cooldown_until = start_time + current_cooldowns.seconds end end |
.setup ⇒ Object
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
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
178 179 180 |
# File 'lib/bitex_bot/robot.rb', line 178 def active_opening_flows? BuyOpeningFlow.active.exists? || SellOpeningFlow.active.exists? end |
#finalise_some_opening_flows ⇒ Object
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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/bitex_bot/robot.rb', line 182 def notify() self.class.logger.error() if Settings.mailer mail = Mail.new do from Settings.mailer.from to Settings.mailer.to subject 'Notice from your robot trader' body end mail.delivery_method(Settings.mailer.method.to_sym, Settings.mailer..symbolize_keys) mail.deliver! end end |
#open_positions? ⇒ Boolean
95 96 97 |
# File 'lib/bitex_bot/robot.rb', line 95 def open_positions? OpenBuy.open.exists? || OpenSell.open.exists? end |
#start_closing_flows ⇒ Object
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_needed ⇒ Object
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bitex_bot/robot.rb', line 114 def start_opening_flows_if_needed return if store.reload.hold? return if active_closing_flows? return if self.class.graceful_shutdown , 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_selling balances = with_cooldown{ Bitstamp.balance } profile = Bitex::Profile.get total_usd = balances['usd_balance'].to_d + profile[:usd_balance] total_btc = balances['btc_balance'].to_d + profile[:btc_balance] store.update_attributes(bitstamp_usd: balances['usd_balance'], bitstamp_btc: balances['btc_balance']) if store.last_warning.nil? || store.last_warning < 30.minutes.ago if store.usd_warning && total_usd <= store.usd_warning notify("USD balance is too low, it's #{total_usd},"\ "make it #{store.usd_warning} to stop this warning.") store.update_attributes(last_warning: Time.now) end if store.btc_warning && total_btc <= store.btc_warning notify("BTC balance is too low, it's #{total_btc},"\ "make it #{store.btc_warning} to stop this warning.") store.update_attributes(last_warning: Time.now) end end return if store.usd_stop && total_usd <= store.usd_stop return if store.btc_stop && total_btc <= store.btc_stop order_book = with_cooldown{ Bitstamp.order_book } transactions = with_cooldown{ Bitstamp.transactions } unless BuyOpeningFlow.create_for_market( balances['btc_available'].to_d, order_book['bids'], transactions, profile[:fee], balances['fee'].to_d ) end unless recent_selling SellOpeningFlow.create_for_market( balances['usd_available'].to_d, order_book['asks'], transactions, profile[:fee], balances['fee'].to_d ) end end |
#store ⇒ Object
The trader has a Store
198 199 200 |
# File 'lib/bitex_bot/robot.rb', line 198 def store @store ||= Store.first || Store.create end |
#sync_closing_flows ⇒ Object
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_flows ⇒ Object
174 175 176 |
# File 'lib/bitex_bot/robot.rb', line 174 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.}:\n\n#{e.backtrace.join("\n")}") BitexBot::Robot.graceful_shutdown = true rescue StandardError => e self.notify("#{e.}:\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 |