# frozen_string_literal: true module Cryptum # Cryptum::OrderBook Namespace module OrderBook # Load in Existing Order Book from File or Generate a New Order Book module Generate # Supported Method Parameters:: # Cryptum::OrderBook::Generate.new( # symbol: 'required - target symbol (e.g. btc-usd)', # this_product: 'required - this_product', # ) 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.%N%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 == :cyan || 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 # Display Usage for this Module 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 end end end