# 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] # Compatible w/ l2update websocket channel # 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 # 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 # 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. # 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 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