Module: Coinbot::WebSock::EventMachine
- Defined in:
- lib/coinbot/web_sock/event_machine.rb
Class Method Summary collapse
-
.help ⇒ Object
Display Usage for this Module.
-
.run(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::WS.run( ).
Class Method Details
.help ⇒ Object
Display Usage for this Module
184 185 186 187 188 |
# File 'lib/coinbot/web_sock/event_machine.rb', line 184 public_class_method def self.help puts "USAGE: logger = #{self}.create() " end |
.run(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::WS.run( )
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 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 |
# File 'lib/coinbot/web_sock/event_machine.rb', line 14 public_class_method def self.run(opts = {}) env = opts[:env] option_choice = opts[:option_choice] terminal_win = opts[:terminal_win] event_history = opts[:event_history] indicator_status = opts[:indicator_status] indicator_history = opts[:indicator_history] max_conn_attempts = 30 conn_attempt = 0 begin conn_attempt += 1 event_history.reconnected = true if conn_attempt > 1 # Automatically Create Bot Configs if they don't # Exist and Initialize Automated Trading Parameters bot_conf = Coinbot::BotConf.read( option_choice: option_choice ) EM.run do # Iterate as fast as possible # This ensures candle timing is accurate # and everything is fast as possible # Defaults to 100ms, 5ms is the lowest possible delay_ms = 5 delay_ms_cast_as_decimal = delay_ms * 0.001 EM.set_quantum(delay_ms) ws = Coinbot::WebSock::Coinbase.connect( option_choice: option_choice, env: env ) ws.on :open do |_event| ws.send( Coinbot::WebSock::Coinbase.( option_choice: option_choice, env: env ) ) end ws.on :message do |event| ai_enabled = bot_conf[:artifical_intelligence] event_history.event = JSON.parse( event.data, symbolize_names: true ) event_history.event_type = event_history.event[:type].to_s.to_sym event_history = Coinbot::Event.parse( env: env, terminal_win: terminal_win, option_choice: option_choice, event_history: event_history, indicator_status: indicator_status, indicator_history: indicator_history, bot_conf: bot_conf, ai_enabled: ai_enabled ) if event_history.candle_reset == true event_history.candle_duration = Coinbot::OrderBook::WrapUp.candle( option_choice: option_choice, env: env, terminal_win: terminal_win, event_history: event_history, indicator_history: indicator_history, bot_conf: bot_conf ) event_history.candle_reset = false end # Decent for Debugging Events # puts event_history.inspect # Decent for Debugging MACD & RSI History # puts indicator_status.inspect # puts indicator_history.inspect # Detect Key Press Events Coinbot::Event::KeyPress.detect(terminal_win: terminal_win) # Only Reload Bot Conf when # r is Pressed if terminal_win.key_press_event.key_r bot_conf = Coinbot::BotConf.read( option_choice: option_choice ) terminal_win.key_press_event.key_r = false end terminal_win.key_press_event.key_u = false if terminal_win.key_press_event.key_u # Only Write Order Book to File when # w is Pressed if terminal_win.key_press_event.key_w order_book_file = event_history.order_book[:path] File.open(order_book_file, 'w') do |f| f.puts event_history.order_book.to_json end terminal_win.key_press_event.key_w = false end if terminal_win.key_press_event.key_x terminal_win.key_press_event.key_x = false Coinbot.exit_gracefully( which_self: self, event_history: event_history, option_choice: option_choice, env: env ) end end ws.on :close do raise Errno::ECONNRESET end EM.add_periodic_timer(delay_ms_cast_as_decimal) do 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.last_order_expires last_order_expires_epoch = Time.parse( event_history.last_order_expires ).strftime('%s').to_i now_epoch = Time.now.strftime('%s').to_i if now_epoch >= last_order_expires_epoch event_history.order_just_expired = true event_history.last_order_expires = nil end end event_history.candle_reset = true if candle_countdown.zero? || candle_countdown.negative? end end rescue Faye::WebSocket::API::ErrorEvent, Errno::ECONNREFUSED, Errno::ECONNRESET => e 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 raise e if conn_attempt > max_conn_attempts sleep 1 retry end rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session Coinbot.exit_gracefully( which_self: self, event_history: event_history, option_choice: option_choice, env: env ) rescue StandardError => e raise e end |