Module: Cryptum::OrderBook::Generate

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

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



101
102
103
104
105
106
107
108
# File 'lib/cryptum/order_book/generate.rb', line 101

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

.new_order_book(opts = {}) ⇒ Object

Supported Method Parameters

Cryptum::OrderBook.generate(

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

)



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
# File 'lib/cryptum/order_book/generate.rb', line 18

public_class_method def self.new_order_book(opts = {})
  start_time = opts[:start_time]
  option_choice = opts[:option_choice]
  env = opts[:env]
  bot_conf = opts[:bot_conf]

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

  order_history_meta = []
  if File.exist?(order_book_file)
    last_order_book = Cryptum::OrderBook.analyze(
      order_book_file: order_book_file,
      option_choice: option_choice
    )
    order_history_meta = last_order_book[:order_history_meta] unless last_order_book[:order_history_meta].empty?
  end

  # Only need to retrieve a product list once / session.
  products = Cryptum::API.get_products(
    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.first

  order_book = {
    path: order_book_file,
    symbol: option_choice.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,
    highest_pie_in_sky_buy_percent: 0.00,
    highest_pie_in_sky_sell_percent: 0.00,
    sequence: -1,
    this_product: this_product,
    portfolio: [],
    fiat_portfolio: [],
    fees: [],
    order_plan: [],
    last_trend_reset: Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z'),
    last_order_exec: Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z'),
    market_trend: {
      buy: 0,
      buy_start: '--',
      buy_end: '--',
      sell: 0,
      sell_start: '--',
      sell_end: '--'
    },
    order_history: [],
    order_history_meta: order_history_meta
  }

  # Order History Retention ---------------------------------------#
  # Instantiate Event History attr_accessible
  # Object to Keep Track of Everything as Events
  # are Parsed.
  event_history = Cryptum::Event::History.new(
    option_choice: option_choice,
    start_time: start_time,
    order_book: order_book
  )

  # Write order_book to file at session initiation
  File.open(order_book_file, 'w') do |f|
    f.puts order_book.to_json
  end

  event_history.order_book = order_book

  event_history
rescue StandardError => e
  raise e
end