# frozen_string_literal: true module Cryptum # This plugin is used to read and update bot conf files. module BotConf # Deserialize Cryptum Bot Conf public_class_method def self.read(opts = {}) option_choice = opts[:option_choice] event_history = opts[:event_history] session_root = option_choice.session_root symbol = option_choice.symbol bot_conf_file = "#{session_root}/etc/bot_confs/#{symbol}_bot_conf.yaml" unless File.exist?(bot_conf_file) FileUtils.cp( "#{session_root}/etc/bot_confs/BOT_CONF.TEMPLATE", bot_conf_file ) end bot_conf = YAML.load_file( bot_conf_file, symbolize_names: true ) ai_enabled = bot_conf[:artifical_intelligence] if ai_enabled && event_history bot_conf = Cryptum::BotConf.recalculate_tpm( option_choice: option_choice, event_history: event_history, bot_conf: bot_conf ) end bot_conf rescue Errno::ENOENT, NoMethodError => e File.open('/tmp/cryptum-errors.txt', 'a') do |f| f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z') f.puts "Module: #{self}" f.puts "#{e}\n\n\n" end retry rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session Cryptum::UI::Exit.gracefully( which_self: self, event_history: event_history, option_choice: option_choice ) rescue StandardError => e # Produce a Stacktrace for anything else Curses.close_screen raise e end # SAUCE 1 # Calculate Target Profit Margin Percent Based Upon # Observed Margins of Change in the Past 24hrs. public_class_method def self.recalculate_tpm(opts = {}) option_choice = opts[:option_choice] event_history = opts[:event_history] bot_conf = opts[:bot_conf] # BE EXTREMELY CAREFUL CHANGING THIS VALUE AS IT DICTATES # THE TARGET PRICE AND SUBSEQUENT TIME IT TAKES FOR AN OPEN # SELL ORDER TO BE TRIGGERED!!! SHOULD NEVER BE > 1 gross_tpm = bot_conf[:target_profit_margin_percent].to_f fees = event_history.order_book[:fees] maker_rate = 0.004 maker_rate = fees[:maker_fee_rate].to_f unless fees.empty? taker_rate = 0.006 taker_rate = fees[:taker_fee_rate].to_f unless fees.empty? # Absolute Bare Minimum TPM (Promotes Volume & Reduces Fees) default_net_tpm = 1.0 + (maker_rate + taker_rate) # Set default_net_tpm if AI is true in bot_conf. low_24h = event_history.order_book[:low_24h].to_f high_24h = event_history.order_book[:high_24h].to_f case option_choice.market_trend_reset when 604_800 # 1W Chart ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) * 7 when 86_400 # 1D Chart ai_net_tpm = (1 - (low_24h / high_24h)) * 100 when 14_400 # 4H Chart ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 6 when 10_800 # 3H Chart ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 8 when 7_200 # 2H Chart ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 12 when 3_600 # 1H Chart ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 24 when 2_700 # 45m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.75 when 1_800 # 30m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.5 when 900 # 15m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.25 when 300 # 5m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.083 when 180 # 3m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.05 when 60 # 1m Chart ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.017 end conservative_tpm_hedge = 0.75 ai_net_tpm *= conservative_tpm_hedge default_net_tpm = ai_net_tpm if ai_net_tpm > default_net_tpm min_gross_tpm = format( '%0.2f', default_net_tpm ) if min_gross_tpm != gross_tpm.to_s bot_conf[:target_profit_margin_percent] = min_gross_tpm.to_f Cryptum::BotConf.update( option_choice: option_choice, bot_conf: bot_conf, key: :target_profit_margin_percent, value: min_gross_tpm.to_f ) end bot_conf rescue Errno::ENOENT, NoMethodError => e File.open('/tmp/cryptum-errors.txt', 'a') do |f| f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z') f.puts "Module: #{self}" f.puts "#{e}\n\n\n" end retry rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session Cryptum::UI::Exit.gracefully( which_self: self, event_history: event_history, option_choice: option_choice ) rescue StandardError => e # Produce a Stacktrace for anything else Curses.close_screen raise e end # Update Key/Value Pair in Bot Conf and Serialize to YAML File public_class_method def self.update(opts = {}) option_choice = opts[:option_choice] bot_conf = opts[:bot_conf] key = opts[:key].to_s.to_sym value = opts[:value] session_root = option_choice.session_root symbol = option_choice.symbol bot_conf_file = "#{session_root}/etc/bot_confs/#{symbol}_bot_conf.yaml" bot_conf[key] = value File.write(bot_conf_file, bot_conf.to_yaml) rescue Errno::ENOENT, NoMethodError => e File.open('/tmp/cryptum-errors.txt', 'a') do |f| f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z') f.puts "Module: #{self}" f.puts "#{e}\n\n\n" end retry rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session Cryptum::UI::Exit.gracefully( which_self: self, option_choice: option_choice ) rescue StandardError => e # Produce a Stacktrace for anything else Curses.close_screen raise e end # Display Usage for this Module public_class_method def self.help puts "USAGE: " end end end