#!/usr/bin/env ruby # frozen_string_literal: true require 'optparse' require 'cryptum' # Parameters available for this driver class Choice attr_accessor :autotrade_percent, :cycles_complete, :driver_name, :proxy, :session_root, :sandbox, :symbol, :tpm, :total_holdings rescue StandardError => e # Produce a Stacktrace for anything else raise e end begin option_choice = Choice.new option_choice.driver_name = File.basename($PROGRAM_NAME) OptionParser.new do |options| options.banner = "USAGE: #{option_choice.driver_name} [opts]" options.on( '-sSYMBOL', '--symbol=SYMBOL', '' ) { |c| option_choice.cycles_complete = c.to_i } options.on( '-hHOLDINGS', '--total-holdings=HOLDINGS', '' ) { |p| option_choice.proxy = p } options.on( '-rPATH', '--session-root=PATH', '' ) { |r| option_choice.session_root = r } options.on( '-tTPM', '--target-profit-margin=TPM', '' ) { |n| option_choice.sandbox = n } end.parse! # Conditions to display cryptum usage if option_choice.symbol.nil? usage = true reason = :symbol end option_choice.session_root = Dir.pwd if option_choice.session_root.nil? if option_choice.autotrade_percent.to_f > 100 usage = true reason = :autotrade_percent end unless Dir.exist?(option_choice.session_root) usage = true reason = :session_root end if usage case reason when :autotrade_percent puts 'ERROR: --autotrade PERCENT value cannot exceed 100' when :symbol puts "ERROR: --symbol Flag is Required.\n\n" when :session_root puts "ERROR: #{option_choice.session_root} does not exist.\n\n" end puts `#{option_choice.driver_name} --help` exit 1 end autotrade_cycle_tot = option_choice.cycles_complete autotrade_cycle_tot = 1 unless option_choice.cycles_complete.to_i.positive? # Initialize the Respective Environment / API Authentication Artifacts env = Cryptum::Option.get_env(option_choice: option_choice) # Read in Bot Conf Values bot_conf = Cryptum::BotConf.read(option_choice: option_choice) products = Cryptum::API.get_products( option_choice: option_choice, env: env ) # crypto = products.last[:base_currency] fiat = products.last[:quote_currency] fiat_portfolio_file = "#{option_choice.session_root}/order_books/#{fiat}_PORTFOLIO.json" # portfolio = Cryptum::API.get_portfolio( # option_choice: option_choice, # env: env, # crypto: crypto, # fiat: fiat, # fiat_portfolio_file: fiat_portfolio_file # ) fiat_portfolio = JSON.parse( File.read(fiat_portfolio_file), symbolize_names: true ) holdings = fiat_portfolio.last[:total_holdings].to_f holdings = option_choice.total_holdings.to_f if option_choice.total_holdings.to_f.positive? autotrade = bot_conf[:autotrade_portfolio_percent].to_f / 100 autotrade = option_choice.autotrade_percent.to_f / 100 if option_choice.autotrade_percent.to_f.positive? gross_tpm = bot_conf[:target_profit_margin_percent].to_f / 100 gross_tpm = option_choice.tpm.to_f if option_choice.tpm.to_f.positive? fees = Cryptum::API.get_fees( option_choice: option_choice, env: env ) taker_fee = format('%0.4f', fees[:taker_fee_rate].to_f) total_cycle_fees = taker_fee.to_f * 2 net_tpm = gross_tpm - total_cycle_fees beautify_holdings = Cryptum.beautify_large_number( value: format('%0.2f', holdings) ) print "Initial Holdings: $#{beautify_holdings} | " print "Autotrade: #{format('%0.2f', autotrade * 100)}% | " print "Gross TPM: #{format('%0.2f', gross_tpm * 100)}% | " print "Fee: #{format('%0.2f', total_cycle_fees * 100)}% | " puts "Net TPM: #{format('%0.2f', net_tpm * 100)}%" (1..autotrade_cycle_tot).each do |autotrade_cycle| risk_alloc = holdings * autotrade bal = risk_alloc bal += bal * net_tpm profit = bal - risk_alloc holdings += profit beautify_risk_alloc = Cryptum.beautify_large_number( value: format('%0.2f', risk_alloc) ) beautify_profit = Cryptum.beautify_large_number( value: format('%0.2f', profit) ) beautify_holdings = Cryptum.beautify_large_number( value: format('%0.2f', holdings) ) print "##{autotrade_cycle} | " print "Risk Alloc: $#{beautify_risk_alloc} | " print "Profit: $#{beautify_profit} | " puts "Holdings: $#{beautify_holdings}" end rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session puts "Interrupt detected in #{self}...goodbye." rescue StandardError => e # Produce a Stacktrace for anything else raise e end