Module: Cryptum::OrderBook::MarketTrend

Defined in:
lib/cryptum/order_book/market_trend.rb

Overview

This Module is used for Market Trend Analysis

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



94
95
96
97
98
# File 'lib/cryptum/order_book/market_trend.rb', line 94

public_class_method def self.help
  puts "USAGE:
   order_trend_indicator_hash = #{self}.status
  "
end

.reset(opts = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cryptum/order_book/market_trend.rb', line 65

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]

  # Only retain past 24 hours of
  # order history meta for expired
  # keep open limit sell orders indefintely
  before_twenty_four_hrs_ago = Time.now - 86_400
  event_history.order_book[:order_history_meta].delete_if do |ohm|
    next unless ohm[:color] == 'white'

    Time.parse(ohm[:done_at]) < before_twenty_four_hrs_ago
  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%z'
  )

  event_history
rescue Interrupt, StandardError => e
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.status(opts = {}) ⇒ Object

Supported Method Parameters

Cryptum::OrderBook::OrderTrend.status( )



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cryptum/order_book/market_trend.rb', line 12

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