Module: Coinbot::Event

Defined in:
lib/coinbot/event.rb,
lib/coinbot/event/buy.rb,
lib/coinbot/event/sell.rb,
lib/coinbot/event/cancel.rb,
lib/coinbot/event/history.rb,
lib/coinbot/event/key_press.rb

Overview

This plugin is used to Cancel Open Limit Orders

Defined Under Namespace

Modules: Buy, Cancel, KeyPress, Sell Classes: History

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



281
282
283
# File 'lib/coinbot/event.rb', line 281

public_class_method def self.help
  constants.sort
end

.parse(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::Event.parse( )



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/coinbot/event.rb', line 18

public_class_method def self.parse(opts = {})
  option_choice = opts[:option_choice]
  env = opts[:env]
  terminal_win = opts[:terminal_win]
  event_history = opts[:event_history]
  indicator_status = opts[:indicator_status]
  indicator_history = opts[:indicator_history]
  bot_conf = opts[:bot_conf]
  ai_enabled = opts[:ai_enabled]

  candles = event_history.order_book[:candles]
  order_history = event_history.order_book[:order_history]
  this_product = event_history.order_book[:this_product]
  fiat = this_product[:quote_currency]
  fiat_portfolio_file = "#{option_choice.repo_root}/order_books/#{fiat}_PORTFOLIO.json"

  # Detect Key Press Events
  Coinbot::Event::KeyPress.detect(terminal_win: terminal_win)

  # Determine if Summary UI needs updated data
  event_history = Coinbot::Portfolio::Balance.refresh(
    env: env,
    option_choice: option_choice,
    terminal_win: terminal_win,
    event_history: event_history,
    fiat_portfolio_file: fiat_portfolio_file,
    indicator_status: indicator_status
  )

  # DETERMINE IF WE'RE INVESTED
  # Get Crypto Portfolio Balance.
  invested = Coinbot::Portfolio::Invested.refresh(
    option_choice: option_choice,
    event_history: event_history
  )

  # If the Terminal Window has been Resized, Resize the UI
  if Curses.cols != terminal_win.cols
    terminal_win.cols = Curses.cols
    terminal_win.ticker_ui_resize = true
    terminal_win.candle_ui_resize = true
  end

  if event_history.event_type == :ticker ||
     terminal_win.ticker_ui_resize

    ticker_event = event_history.ticker_event = event_history.event if event_history.event_type == :ticker
    ticker_event = event_history.ticker_event if terminal_win.ticker_ui_resize
    Coinbot::UI::Ticker.refresh(
      option_choice: option_choice,
      start_time: event_history.start_time,
      ticker_win: terminal_win.ticker_section,
      key_press_event: terminal_win.key_press_event,
      order_book: event_history.order_book,
      event: ticker_event
    )
  end

  candle_countdown = Coinbot::UI::Timer.refresh(
    candle_duration: event_history.candle_duration,
    timer_win: terminal_win.timer_section,
    candle_begin_time: event_history.order_book[:candles].last[:begin_time],
    key_press_event: terminal_win.key_press_event
  )

  if event_history.event_type == :l2update ||
     terminal_win.candle_ui_resize

    candle_event = event_history.candle_event = event_history.event if event_history.event_type == :l2update
    candle_event = event_history.candle_event if terminal_win.candle_ui_resize
    Coinbot::UI::Candle.refresh(
      candle_win: terminal_win.candle_section,
      key_press_event: terminal_win.key_press_event,
      order_book: event_history.order_book,
      option_choice: option_choice,
      event: candle_event,
      indicator_status: indicator_status,
      indicator_history: indicator_history,
      bot_conf: bot_conf
    )
  end

  # REFRESH INDICATOR STATUSes FOR _EVERY EVENT_ ----------------#
  # Indicate when Golden or Death Crosses are Triggered
  Coinbot::OrderBook::EMACross.status(
    order_book: event_history.order_book,
    indicator_status: indicator_status,
    invested: invested
  )

  # Indicate Position of MACD
  Coinbot::OrderBook::MACD.status(
    indicator_status: indicator_status,
    macd_history: indicator_history.macd,
    candles: candles,
    invested: invested
  )

  # Indicate Position of RSI
  Coinbot::OrderBook::RSI.status(
    indicator_status: indicator_status,
    invested: invested,
    rsi_history: indicator_history.rsi,
    candles: candles,
    bot_conf: bot_conf
  )

  # Determine if We're Stuck in Our Position
  # IF INVESTED.
  stuck_in_pos_status = Coinbot::OrderBook::StuckInPosition.status(
    order_book: event_history.order_book,
    indicator_status: indicator_status,
    invested: invested,
    bot_conf: bot_conf
  )
  #--------------------------------------------------------------#

  Coinbot::UI::Portfolio.refresh(
    option_choice: option_choice,
    portfolio_win: terminal_win.portfolio_section,
    event_history: event_history,
    key_press_event: terminal_win.key_press_event,
    bot_conf: bot_conf,
    stuck_in_pos_status: stuck_in_pos_status
  )

  # Derive Action Probabilities
  action_probability_status = Coinbot::ProbabilityEngine.calculate(
    event_history: event_history,
    indicator_status: indicator_status,
    bot_conf: bot_conf,
    invested: invested
  )

  # Refresh Status Section in UI w/ Updated Indicators
  Coinbot::UI::Status.refresh(
    status_win: terminal_win.status_section,
    key_press_event: terminal_win.key_press_event,
    indicator_status: indicator_status
  )

  # Refresh Action Probability Section in UI w/ New Action
  candle_length = candles.length
  ema_high = bot_conf[:exponential_moving_avg][:high].to_i
  Coinbot::UI::ActionProbability.refresh(
    probability_win: terminal_win.probability_section,
    key_press_event: terminal_win.key_press_event,
    candle_length: candle_length,
    ema_high: ema_high,
    action_probability_status: action_probability_status,
    option_choice: option_choice,
    ai_enabled: ai_enabled
  )

  # Refresh Command Section for Coinbot Session Usage
  Coinbot::UI::Command.refresh(
    command_win: terminal_win.command_section,
    key_press_event: terminal_win.key_press_event
  )

  # AUTOTRADING SECTION / ORDER SUBMISSION
  # base_min_size = this_product[:base_min_size]
  # crypto_min_order_size_decimal = base_min_size.to_s.split('.')[-1].length
  min_market_funds = this_product[:min_market_funds]
  base_increment = this_product[:base_increment]
  quote_increment = this_product[:quote_increment]
  # crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length
  fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length

  last_ticker_price = event_history.order_book[:ticker_price].to_f
  second_to_last_ticker_price = event_history.order_book[:ticker_price_second_to_last].to_f
  third_to_last_ticker_price = event_history.order_book[:ticker_price_third_to_last].to_f

  if option_choice.autotrade &&
     event_history. == false &&
     last_ticker_price.positive? &&
     second_to_last_ticker_price.positive? &&
     third_to_last_ticker_price.positive? &&
     fiat_smallest_decimal.positive?

    # 1. Acquire Highest Probability Action
    proposed_action = action_probability_status.sort_by do |_k, v|
      v[:weight]
    end.reverse.to_h.keys.first.to_s.downcase.to_sym

    # Default order type
    order_type = :pie

    case proposed_action
    when :cancel, :gtfo
      event_history.event_type = :cancel if proposed_action == :cancel
      event_history.event_type = :gtfo if proposed_action == :gtfo

      order_type = :tpm if proposed_action == :cancel
      order_type = :gtfo if proposed_action == :gtfo

      if event_history.order_canceled == false
        Coinbot::Event::Cancel.open_orders(
          option_choice: option_choice,
          env: env,
          order_type: order_type,
          event_history: event_history
        )
        # event_history.order_just_expired = true
      end

      proposed_action = :buy unless invested
      proposed_action = :sell if invested
    end

    case proposed_action
    when :buy
      event_history = Coinbot::Event::Buy.crypto(
        option_choice: option_choice,
        env: env,
        bot_conf: bot_conf,
        stuck_in_pos_status: stuck_in_pos_status,
        event_history: event_history,
        order_type: order_type,
        fiat_smallest_decimal: fiat_smallest_decimal,
        order_history: order_history,
        min_market_funds: min_market_funds,
        indicator_status: indicator_status
      )
    when :sell
      event_history = Coinbot::Event::Sell.crypto(
        option_choice: option_choice,
        env: env,
        bot_conf: bot_conf,
        stuck_in_pos_status: stuck_in_pos_status,
        event_history: event_history,
        order_type: order_type,
        fiat_smallest_decimal: fiat_smallest_decimal,
        order_history: order_history,
        min_market_funds: min_market_funds,
        indicator_status: indicator_status,
        quote_increment: quote_increment
      )
    end

    terminal_win.key_press_event.key_u = true
  end

  event_history
rescue LoadError => e
  # This happens when autoloading modules fail.
  File.open('/tmp/coinbot-errors.txt', 'a') do |f|
    f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z')
    f.puts "#{self}\n#{e}\n\n\n"
  end

  retry
rescue Interrupt
  # Exit Gracefully if CTRL+C is Pressed During Session
  Coinbot.exit_gracefully(which_self: self)
rescue StandardError => e
  raise e
ensure
  $stdout.flush
end