Module: Coinbot::Portfolio::Balance

Defined in:
lib/coinbot/portfolio/balance.rb

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



152
153
154
155
156
# File 'lib/coinbot/portfolio/balance.rb', line 152

public_class_method def self.help
  puts "USAGE:
   event_history.order_book = #{self}.crypto()
  "
end

.refresh(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::Event::Update.summary( )



12
13
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/coinbot/portfolio/balance.rb', line 12

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]
  indicator_status = opts[:indicator_status]

  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 ||
     event_history.last_opened_order_id != event_history.this_opened_order_id ||
     event_history.order_just_expired ||
     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.

    # 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 (
                          event_history.order_just_expired ||
                          etype == :done
                       ) && !manual_refresh

    event_history. = osubmit

    if indicator_status.stuck_in_position
      sip_color = indicator_status.stuck_in_position[:color].to_sym
      event_history.order_just_expired = false if sip_color == :green
    end
    # ---------------------------------------------- #

    enotes = event_history.event_notes
    enotes = "{ \"event_type\": \"#{etype}\", \"cancel\": \"#{ocancel}\", \"submitted\": \"#{osubmit}\", \"this_opened_order_id\": \"#{event_history.this_opened_order_id}\", \"last_opened_order_id\": \"#{event_history.last_opened_order_id}\", \"last_order_expires\": \"#{event_history.last_order_expires}\", \"event\": \"#{event_history.event}\", \"order_just_expired\": \"#{event_history.order_just_expired}\" }" if option_choice.proxy
    event_history.event_notes = enotes

    portfolio = Coinbot::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 = Coinbot::API.get_order_history(
      option_choice: option_choice,
      env: env
    )
    event_history.order_book[:order_history] = order_history unless portfolio.empty?

    ojh = event_history.order_book[:order_justification_history]
    order_history.each do |order|
      exists_in_ojh = ojh.select { |eojh| eojh[:id] == order[:id] }

      if exists_in_ojh.empty?
        # Tag orders that don't exist in
        # order_justification_history as manual
        this_o = {}
        this_o[:id] = order[:id]
        this_o[:order_action] = order[:side]
        this_o[:order_status] = order[:status]
        this_o[:order_tag] = :manual
        ojh.push(this_o)
      else
        # Update order_justification_history
        # with latest order statuses
        exists_in_ojh.each do |coinbot_order|
          coinbot_order[:order_status] = order[:status] if coinbot_order[:id] == order[:id]
        end
      end
    end
    event_history.order_book[:order_justification_history] = ojh

    fees = Coinbot::API.get_fees(
      option_choice: option_choice,
      env: env
    )
    event_history.order_book[:fees] = fees unless fees.empty?

    # First Event to Refresh No Longer Needed
    event_history.first_event = false

    # Reconnected Event to Refresh No Longer Needed
    event_history.reconnected = false

    # Ensure last order id matches this order id
    event_history.last_opened_order_id = event_history.this_opened_order_id
  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