# frozen_string_literal: true require 'io/console' require 'json' require 'logger' require 'tty-spinner' module Cryptum # This module is used to define the Order Book Data Structure module OrderBook module Generate # Supported Method Parameters:: # Cryptum::OrderBook.generate( # symbol: 'required - target symbol (e.g. btc-usd)', # this_product: 'required - this_product', # ) 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.last 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 ) event_history.order_book = order_book event_history rescue StandardError => e raise e end # Display Usage for this Module 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 end end end