Module: Cryptum::OrderBook::Generate

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

Overview

Load in Existing Order Book from File or Generate a New Order Book

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



118
119
120
121
122
123
124
125
# File 'lib/cryptum/order_book/generate.rb', line 118

public_class_method def self.help
  puts "USAGE:
    order_book = #{self}.new(
      symbol: 'required - target symbol (e.g. btc-usd)',
      this_product: 'required - this_product'
    )
  "
end

.new(opts = {}) ⇒ Object

Supported Method Parameters

Cryptum::OrderBook::Generate.new(

symbol: 'required - target symbol (e.g. btc-usd)',
this_product: 'required - this_product',

)



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
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cryptum/order_book/generate.rb', line 14

public_class_method def self.new(opts = {})
  option_choice = opts[:option_choice]
  env = opts[:env]

  session_root = option_choice.session_root
  symbol = option_choice.symbol
  order_book_file = "#{session_root}/order_books/#{symbol}.ORDER_BOOK.json"

  # Only need to retrieve a product list once / session.
  products = Cryptum::API::Products.get(
    option_choice: option_choice,
    env: env
  )
  this_product_arr = products.select do |product|
    product if product[:id] == option_choice.symbol.to_s.gsub('_', '-').upcase
  end
  this_product = this_product_arr.last

  ftimestr = '%Y-%m-%d %H:%M:%S%z'

  order_book = {
    path: order_book_file,
    symbol: symbol,
    open_24h: 0.00,
    high_24h: 0.00,
    low_24h: 0.00,
    volume_24h: 0.00,
    ticker_price: 0.00,
    ticker_price_second_to_last: 0.00,
    ticker_price_third_to_last: 0.00,
    sequence: -1,
    this_product: this_product,
    portfolio: [],
    fiat_portfolio: [],
    fees: [],
    order_plan: [],
    last_trend_reset: Time.now.strftime(ftimestr),
    last_order_exec: Time.now.strftime(ftimestr),
    market_trend: {
      buy: 0,
      buy_start: '--',
      buy_end: '--',
      sell: 0,
      sell_start: '--',
      sell_end: '--'
    },
    order_history: [],
    order_history_meta: []
  }

  # Order History Retention ---------------------------------------#
  # Instantiate Event History attr_accessible
  # Object to Keep Track of Everything as Events
  # are Parsed.
  if File.exist?(order_book_file)
    order_book = JSON.parse(
      File.read(order_book_file),
      symbolize_names: true
    )

    # Only keep order history meta for those hashes
    # that exist in the last order history response
    # :white ones will be kept for at least 24 hrs
    # and handled via Cryptum::OrderBook::MarketTrend.reset
    # This exist primarily to clean up "zombie" meta
    # data entries if the bot crashes for whatever reason.
    order_book[:order_history_meta].keep_if do |ohm|
      order_book[:order_history].find do |oh|
        (oh[:id] == ohm[:buy_order_id] || oh[:id] == ohm[:sell_order_id]) && (
          ohm[:color].to_sym == :green ||
          ohm[:color].to_sym == :magenta ||
          ohm[:color].to_sym == :red ||
          ohm[:color].to_sym == :yellow
        )
      end
    end
  end

  event_history = Cryptum::Event::History.new(
    option_choice: option_choice,
    order_book: order_book
  )
  event_history.order_book = order_book

  if option_choice.reset_timers
    event_history.order_book[:order_plan] = []
    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(ftimestr)
    event_history.order_book[:last_order_exec] = Time.now.strftime(ftimestr)

    msg = 'Market Trend and Order Timers Reset'
  else
    msg = 'Market Trend and Order Timers Preserved from Previous Session'
  end
  Cryptum::Log.append(level: :info, msg: msg, which_self: self, event_history: event_history)

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