Skip to content

Instantly share code, notes, and snippets.

@rgaufman
Last active April 17, 2021 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgaufman/84942b97abcfa419895116bb495fa177 to your computer and use it in GitHub Desktop.
Save rgaufman/84942b97abcfa419895116bb495fa177 to your computer and use it in GitHub Desktop.
API_KEY = 'xxx'
SECRET_KEY = 'xxx'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'cryptocompare'
gem 'binance-ruby'
gem 'activesupport'
end
require 'active_support/all'
# How often to show your account balance in seconds
DISPLAY_INTERVAL = 600
# How long to wait before restarting a bot that's stuck on a bridge currency
MAX_BRIDGE_CURRENCY_TIME = 30.minutes
# How long to wait before forcing the bot to sell the current currency
MAX_COIN_CURRENCY_TIME = 4.hours
# The desired Fiat currency to show in the balance outputs
FIAT_CURRENCY = 'GBP'
# If the Fiat balance drops below this amount, immediately sell the coin back to Fiat
CASH_OUT_FIAT = 10_000
Binance::Api::Configuration.api_key = API_KEY
Binance::Api::Configuration.secret_key = SECRET_KEY
class BotRunner
def initialize
end
def get_balances
5.times do
begin
info = Binance::Api.info!
balances = info[:balances]&.reject { |b| b[:free].to_i.zero? }
return balances if balances&.size&.positive?
rescue Binance::Api::Error => e
puts "!!! #{e.message}, retrying..."
end
sleep 5
end
end
def currency_convert(amount, from, to)
(Cryptocompare::Price.find(from, to)[from][to] * amount).round(6)
end
def start_bot
restart_bot
end
def restart_bot(log: true)
if @pid
puts '!!! Killing the bot'
# Process.kill('TERM', @pid)
# system('rm ./data/backtest_cache.db ./data/crypto_trading.db')
end
puts '*** Starting the bot'
# cmd = 'python3 -m binance_trade_bot > /dev/null &>/dev/null'
# @pid = spawn cmd
end
def run
loop do
run_once
sleep DISPLAY_INTERVAL
end
end
def run_once
balances = get_balances
return if balances.size.zero? || !balances.is_a?(Array)
btc = 0
fiat = 0
results = balances&.map do |b|
coin = b[:asset]
free = b[:free].to_i
btc_ = currency_convert(free, coin, 'BTC')
fiat_ = currency_convert(free, coin, FIAT_CURRENCY)
btc += btc_
fiat += fiat_
[coin, "#{btc.round(5)} BTC", "#{fiat.round(2)} #{FIAT_CURRENCY}"] unless fiat_ < 1
end
percent_change_btc = \
if !@previous_btc || btc == @previous_btc
'0%'
elsif btc > @previous_btc
"+#{(1 - (@previous_btc / btc)).round(4)}%"
else
"#{(1 - (@previous_btc / btc)).round(4)}%"
end
coins = results.size > 1 ? results : balances[0][:asset]
puts "#{Time.now} --- #{fiat.round(0)} #{FIAT_CURRENCY} :: #{btc.round(4)} BTC #{percent_change_btc} :::: #{coins}"
@last_not_usdt = Time.now unless balances[0][:asset] == 'USDT'
@current_coin_since = Time.now unless @current_coin == balances[0][:asset]
@current_coin = balances[0][:asset]
@previous_btc = btc
@previous_fiat = fiat
# Stuck on a coin, kill the bot
if @current_coin_since && @current_coin_since < MAX_COIN_CURRENCY_TIME
puts '!!! Stuck on Current Currency'
restart_bot
@current_coin_since = Time.now
end
# Stuck on USDT, kill the bot
if @last_not_usdt && @last_not_usdt < MAX_BRIDGE_CURRENCY_TIME
puts '!!! Stuck on Bridge Currency'
restart_bot
@last_not_usdt = Time.now
end
end
end
BotRunner.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment