# 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] taker_rate = 0.5 taker_rate = fees[:taker_fee_rate].to_f unless fees.empty? taker_fee = format('%0.2f', taker_rate * 100) default_net_tpm = 3 if ai_enabled low_24h = event_history.order_book[:low_24h].to_f high_24h = event_history.order_book[:high_24h].to_f ai_net_tpm = (1 - (low_24h / high_24h)) * 100 default_net_tpm = ai_net_tpm if ai_net_tpm > default_net_tpm end min_gross_tpm = format( '%0.2f', (taker_fee.to_f * 2) + 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