# frozen_string_literal: true module Cryptum # Cryptum::OrderBook Namespace module OrderBook # This Module is used for Market Trend Analysis 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] # Compatible w/ l2update_batch websocket channel buy_count = event[:changes].select { |change| change.first == 'buy' }.length sell_count = event[:changes].select { |change| change.first == 'sell' }.length event_history.order_book[:market_trend][:buy] += buy_count event_history.order_book[:market_trend][:sell] += sell_count 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 motivation = 'BUYING THE DIP' indicator_hash[:color] = :red if event_history.bullish_trend motivation = 'FOMO' if event_history.bullish_trend indicator_hash[:color] = :green end trend = "BUYS #{Cryptum.up_arrow} BY #{order_difference.to_i}" indicator_hash[:status] = "B#{Cryptum.up_arrow}" elsif buy_total < sell_total order_difference = sell_total - buy_total motivation = 'REKT' indicator_hash[:color] = :red if event_history.bullish_trend motivation = 'TAKING PROFITS' if event_history.bullish_trend indicator_hash[:color] = :green end trend = "SELLS #{Cryptum.up_arrow} BY #{order_difference.to_i}" indicator_hash[:status] = "S#{Cryptum.up_arrow}" else # This Condition is Met When candle_buy_tot == candle_sell_tot motivation = 'FLAT' indicator_hash[:color] = :yellow trend = 'BUYS == SELLS' indicator_hash[:status] = "F#{Cryptum.up_arrow}" end indicator_hash[:ui] = "Market is #{motivation} | #{trend}" indicator_status.market_trend = indicator_hash rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history) 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. # event_history = opts[:event_history] # # 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 # # 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 Interrupt, StandardError => e # Cryptum::Log.append(level: :error, msg: e, which_self: self) # end # Display Usage for this Module public_class_method def self.help puts "USAGE: order_trend_indicator_hash = #{self}.status " end end end end