# frozen_string_literal: true require 'logger' module Cryptum # This plugin is used to instantiate a Cryptum logger with a custom message format module Portfolio module Balance # Supported Method Parameters:: # Cryptum::Event::Update.summary( # ) public_class_method def self.refresh(opts = {}) option_choice = opts[:option_choice] env = opts[:env] terminal_win = opts[:terminal_win] event_history = opts[:event_history] fiat_portfolio_file = opts[:fiat_portfolio_file] this_product = event_history.order_book[:this_product] crypto = this_product[:base_currency] fiat = this_product[:quote_currency] if event_history.first_event || event_history.reconnected || event_history.event_type == :received || event_history.event_type == :open || event_history.event_type == :done || event_history.event_type == :match || event_history.event_type == :change || event_history.event_type == :activate || terminal_win.key_press_event.key_u etype = event_history.event_type manual_refresh = terminal_win.key_press_event.key_u etype = :manual_refresh if manual_refresh etype = :first_event if event_history.first_event etype = :reconnected if event_history.reconnected event_history.event_type = etype # Prevent Multiple Order Events from Occuring --- # ocancel = event_history.order_canceled osubmit = event_history.order_submitted # POTENTIAL STOP IN ORDERS WHEN # event_history.order_canceled = true # Verify it's not due to EMA being red. # ocancel = false if (etype == :open || etype == :done) && # osubmit == false && # !manual_refresh ocancel = false if etype == :done && osubmit == true && !manual_refresh event_history.order_canceled = ocancel osubmit = false if etype == :done && !manual_refresh event_history.order_submitted = osubmit # ---------------------------------------------- # enotes = event_history.event_notes enotes = "{ \"event_type\": \"#{etype}\", \"cancel\": \"#{ocancel}\", \"submitted\": \"#{osubmit}\" }" if option_choice.proxy event_history.event_notes = enotes portfolio = Cryptum::API.get_portfolio( option_choice: option_choice, env: env, crypto: crypto, fiat: fiat, fiat_portfolio_file: fiat_portfolio_file, event_notes: event_history.event_notes ) event_history.order_book[:portfolio] = portfolio unless portfolio.empty? order_history = Cryptum::API.get_order_history( option_choice: option_choice, env: env ) event_history.order_book[:order_history] = order_history unless order_history.empty? fees = Cryptum::API.get_fees( option_choice: option_choice, env: env ) event_history.order_book[:fees] = fees unless fees.empty? terminal_win.key_press_event.key_u = false if manual_refresh # First Event to Refresh No Longer Needed event_history.first_event = false # Reconnected Event to Refresh No Longer Needed event_history.reconnected = false end # Always reload fiat portfolio as it's shared # by all sessions (unless it's empty) fpf = '' fpf = File.read(fiat_portfolio_file) unless File.empty?( fiat_portfolio_file ) unless fpf.empty? event_history.order_book[:fiat_portfolio] = JSON.parse( fpf, symbolize_names: true ) end event_history rescue StandardError => e raise e end # Display Usage for this Module public_class_method def self.help puts "USAGE: event_history.order_book = #{self}.crypto() " end end end end