# frozen_string_literal: true module Cryptum # This plugin is used to indicate if the order trend module OrderBook module MarketTrend # Supported Method Parameters:: # Cryptum::OrderBook::OrderTrend.status( # ) public_class_method def self.status(opts = {}) event_history = opts[:event_history] event = opts[:event] indicator_status = opts[:indicator_status] buy_or_sell = event[:changes].first[0].to_s.to_sym case buy_or_sell when :buy event_history.order_book[:market_trend][:buy] += 1 when :sell event_history.order_book[:market_trend][:sell] += 1 else raise "UNKNOWN Value in event[:changes] => #{buy_or_sell}" end indicator_hash = {} buy_total = event_history.order_book[:market_trend][:buy].to_i indicator_hash[:buy] = buy_total sell_total = event_history.order_book[:market_trend][:sell].to_i indicator_hash[:sell] = sell_total # order_difference = 0 if buy_total > sell_total order_difference = buy_total - sell_total # Too Expensive # order_difference_out = Cryptum.beautify_large_number( # value: order_difference # ).to_i indicator_hash[:color] = :green indicator_hash[:ui] = "BUYS UP BY #{order_difference.to_i}" indicator_hash[:status] = "B#{Cryptum.up_arrow}" elsif buy_total < sell_total order_difference = sell_total - buy_total # Too Expensive # order_difference_out = Cryptum.beautify_large_number( # value: order_difference # ).to_i indicator_hash[:color] = :red indicator_hash[:ui] = "SELLS UP BY #{order_difference.to_i}" indicator_hash[:status] = "S#{Cryptum.up_arrow}" else # This Condition is Met When candle_buy_tot == candle_sell_tot indicator_hash[:color] = :yellow indicator_hash[:ui] = 'BUYS == SELLS' indicator_hash[:status] = "F#{Cryptum.up_arrow}" end indicator_status.market_trend = indicator_hash rescue StandardError => e raise e end public_class_method def self.reset(opts = {}) # IT IS ABSOLUTELY CRITICAL THIS METHOD IS AS FAST AS POSSIBLE # TO AVOID TICKER PRICE SYNCING ISSUES. option_choice = opts[:option_choice] # terminal_win = opts[:terminal_win] event_history = opts[:event_history] bot_conf = opts[:bot_conf] ai_enabled = bot_conf[:artifical_intelligence] gross_tpm = bot_conf[:target_profit_margin_percent].to_f # order_history = event_history.order_book[:order_history] # order_history_meta = event_history.order_book[:order_history_meta] # Only retain past 24 hours of # order history meta for bought, sold, and expired # keep open limit sell orders indefintely # before_twenty_four_hrs_ago = Time.now - 86_400 # order_history_meta.delete_if do |ohm| # order_history.find do |oh| # next unless oh[:done_at] # Time.parse(oh[:done_at]) < before_twenty_four_hrs_ago && ( # oh[:id] == ohm[:buy_order_id] || # oh[:id] == ohm[:sell_order_id] # ) && ( # ohm[:color].to_sym == :white || # ohm[:color].to_sym == :green || # ohm[:color].to_sym == :cyan || # ohm[:color].to_sym == :red # ) # end # end # Only keep order history meta for those # hashes that exist in the last order history # response # order_history_meta.keep_if do |ohm| # order_history.find do |oh| # (oh[:id] == ohm[:buy_order_id] || oh[:id] == ohm[:sell_order_id]) && ( # ohm[:color].to_sym == :white || # ohm[:color].to_sym == :green || # ohm[:color].to_sym == :yellow # ) # end # end # event_history.order_book[:order_history_meta] = order_history_meta # Refactor TPM to be 0.01 > than fee tier, # particularly as fee tier goes up or down fees = event_history.order_book[:fees] maker_rate = 0.4 maker_rate = fees[:maker_fee_rate].to_f unless fees.empty? maker_fee = format('%0.2f', maker_rate * 100) taker_rate = 0.6 taker_rate = fees[:taker_fee_rate].to_f unless fees.empty? taker_fee = format('%0.2f', taker_rate * 100) # 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 default_net_tpm = 1 if ai_enabled # 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 default_net_tpm = ai_net_tpm if ai_net_tpm > default_net_tpm end min_gross_tpm = format( '%0.2f', (maker_fee.to_f + taker_fee.to_f) + default_net_tpm ) if ai_enabled && 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 # Reset Market Trend Counter event_history.order_book[:market_trend][:buy] = 0 event_history.order_book[:market_trend][:sell] = 0 event_history.order_book[:last_trend_reset] = Time.now.strftime( '%Y-%m-%d %H:%M:%S.%N%z' ) rescue StandardError => e raise e end # Display Usage for this Module public_class_method def self.help puts "USAGE: order_trend_indicator_hash = #{self}.status " end end end end