Module: Coinbot::UI::Candle
- Defined in:
- lib/coinbot/ui/candle.rb
Class Method Summary collapse
-
.help ⇒ Object
Display Usage for this Module.
-
.refresh(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::UI::Candle.refresh( order_book: 'required - Order Book Data Structure', event: 'required - Event from Coinbase Web Socket' ).
Class Method Details
.help ⇒ Object
Display Usage for this Module
511 512 513 514 515 516 517 518 |
# File 'lib/coinbot/ui/candle.rb', line 511 public_class_method def self.help puts "USAGE: #{self}.refresh( order_book: 'required - Order Book Data Structure', event: 'required - Event from Coinbase Web Socket' ) " end |
.refresh(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::UI::Candle.refresh(
order_book: 'required - Order Book Data Structure', event: 'required - Event from Coinbase Web Socket'
)
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 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/coinbot/ui/candle.rb', line 15 public_class_method def self.refresh(opts = {}) option_choice = opts[:option_choice] candle_win = opts[:candle_win] order_book = opts[:order_book] key_press_event = opts[:key_press_event] event = opts[:event] indicator_status = opts[:indicator_status] indicator_history = opts[:indicator_history] bot_conf = opts[:bot_conf] ema_low = bot_conf[:exponential_moving_avg][:low].to_i ema_high = bot_conf[:exponential_moving_avg][:high].to_i last_ticker_price = order_book[:ticker_price].to_f candles = order_book[:candles] this_product = order_book[:this_product] quote_increment = this_product[:quote_increment] fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length fiat = option_choice.symbol.to_s.upcase.split('_').last.to_sym fiat_symbol = '?' fiat_symbol = '$' if fiat == :USD fiat_symbol = "\u20ac" if fiat == :EUR candle_period = candles.last[:period] candle_period_out = "C:#{candle_period}" last_order_price = order_book[:last_order_price].to_f order_trend_indicator_hash = Coinbot::OrderBook::OrderTrend.status( order_book: order_book, event: event, indicator_status: indicator_status ) # candle_buy_tot = order_trend_indicator_hash[:candle_buy_tot] # candle_sell_tot = order_trend_indicator_hash[:candle_sell_tot] order_trend_color = order_trend_indicator_hash[:color] order_trend_out = order_trend_indicator_hash[:ui] last_order_price = order_trend_indicator_hash[:last_order_price] last_order_size = order_trend_indicator_hash[:last_order_size] # TODO: Determine if this should be here return order_book if last_order_size.zero? # Each hash in the candles array represents # one minute of candle history # candle_begin_time = Time.parse( # candles.last[:begin_time] # ) # now_time = Time.now # Calculate EMA, MACD, & RSI in Realtime. sma_nine = Coinbot::OrderBook::SMA.calculate( candle_tot: 9, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:sma_nine] = sma_nine sma_fifty = Coinbot::OrderBook::SMA.calculate( candle_tot: 50, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:sma_fifty] = sma_fifty sma_one_eighty = Coinbot::OrderBook::SMA.calculate( candle_tot: 180, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:sma_one_eighty] = sma_one_eighty ema_twelve = Coinbot::OrderBook::EMA.calculate( candle_tot: 12, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:ema_twelve] = ema_twelve ema_twenty_six = Coinbot::OrderBook::EMA.calculate( candle_tot: 26, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:ema_twenty_six] = ema_twenty_six ema_h_avg = Coinbot::OrderBook::EMA.calculate( candle_tot: ema_high, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:ema_high_avg] = ema_h_avg ema_l_avg = Coinbot::OrderBook::EMA.calculate( candle_tot: ema_low, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:ema_low_avg] = ema_l_avg ema_one_eighty = Coinbot::OrderBook::EMA.calculate( candle_tot: 180, candle_key: :candle_close, candles: candles, last_value: last_ticker_price, this_product: this_product ) candles.last[:ema_one_eighty] = ema_one_eighty indicator_history.macd = Coinbot::OrderBook::MACD.calculate( this_product: this_product, last_value: last_ticker_price, candles: candles, macd_history: indicator_history.macd ) candles.last[:macd_history] = indicator_history.macd macd_fast_avg = indicator_history.macd.last[:macd_fast_avg] macd_slow_avg = indicator_history.macd.last[:macd_slow_avg] macd = indicator_history.macd.last[:macd] signal = indicator_history.macd.last[:signal] histogram = indicator_history.macd.last[:histogram] indicator_history.rsi = Coinbot::OrderBook::RSI.calculate( last_ticker_price: last_ticker_price, candles: candles, rsi_history: indicator_history.rsi ) candles.last[:rsi_history] = indicator_history.rsi rsi = indicator_history.rsi.last[:rsi] # Retrieve Latest Pie-in-the-Sky Recommendation pie_in_sky_buy_percent = bot_conf[:pie_in_sky_buy_percent] highest_pie_in_sky_buy_percent = order_book[:highest_pie_in_sky_buy_percent] pie_in_sky_sell_percent = bot_conf[:pie_in_sky_sell_percent] highest_pie_in_sky_sell_percent = order_book[:highest_pie_in_sky_sell_percent] # Determine what color the opening price should be # based upon previous close price candle_open = candles.last[:candle_open].to_f candles.last[:candle_open] = last_ticker_price if candle_open.zero? candle_highest = candles.last[:candle_highest].to_f if last_ticker_price > candle_highest candles.last[:candle_highest] = last_ticker_price candle_highest = candles.last[:candle_highest].to_f end candle_highest_out = format("%0.#{fiat_smallest_decimal}f", candle_highest) candle_lowest = candles.last[:candle_lowest].to_f if last_ticker_price < candle_lowest || candle_lowest.zero? candles.last[:candle_lowest] = last_ticker_price candle_lowest = candles.last[:candle_lowest].to_f end candle_lowest_out = format("%0.#{fiat_smallest_decimal}f", candle_lowest) order_highest = candles.last[:order_highest].to_f if last_order_price > order_highest candles.last[:order_highest] = last_order_price order_highest = candles.last[:order_highest].to_f end order_highest_out = format("%0.#{fiat_smallest_decimal}f", order_highest) order_lowest = candles.last[:order_lowest].to_f if last_order_price < order_lowest || order_lowest.zero? candles.last[:order_lowest] = last_order_price order_lowest = candles.last[:order_lowest].to_f end order_lowest_out = format("%0.#{fiat_smallest_decimal}f", order_lowest) # candle_time_remaining = (candle_begin_time + option_choice.candle_duration) - now_time # candle_time_remaining_out = format('%0.4f', candle_time_remaining) candle_margin = (1 - (candle_open / last_ticker_price)) * 100 candle_open_out = "#{Coinbot.open_symbol} #{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", candle_open)}" last_ticker_price_str = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", last_ticker_price)}" if candle_margin.positive? candle_margin_color = :green candle_margin_out = "#{Coinbot.up_arrow} #{format('%0.4f', candle_margin)}%" last_ticker_price_color = :green last_ticker_price_out = "#{Coinbot.up_arrow} #{last_ticker_price_str}" elsif candle_margin.negative? candle_margin_color = :red candle_margin_out = "#{Coinbot.down_arrow}#{format('%0.4f', candle_margin)}%" last_ticker_price_color = :red last_ticker_price_out = "#{Coinbot.down_arrow} #{last_ticker_price_str}" else candle_margin_color = :yellow candle_margin_out = "#{Coinbot.flat_arrow} #{format('%0.4f', candle_margin)}%" last_ticker_price_color = :yellow last_ticker_price_out = "#{Coinbot.flat_arrow} #{last_ticker_price_str}" end pie_buy_str = "#{pie_in_sky_buy_percent}% | #{highest_pie_in_sky_buy_percent}%" pie_sell_str = "#{pie_in_sky_sell_percent}% | #{highest_pie_in_sky_sell_percent}%" # TODO: Everything Above this Line Needs to be Indicators ^ # UI col_just1 = 12 col_just2 = 14 col_just3 = 21 col_just3_alt = (Curses.cols - Coinbot::UI.col_third) - 1 col_just4 = (Curses.cols - Coinbot::UI.col_fourth) - 1 Coinbot::UI.detect_key_press_in_ui( key_press_event: key_press_event, ui_win: candle_win ) # ROW 1 out_line_no = 0 Coinbot::UI.line( ui_win: candle_win, out_line_no: out_line_no ) out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :white, style: :bold, string: candle_period_out.ljust(col_just1) ) candle_win.setpos(out_line_no, Coinbot::UI.col_second) Coinbot::UI.colorize( ui_win: candle_win, color: candle_margin_color, style: :bold, string: candle_margin_out.ljust(col_just2) ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :cyan, style: :bold, string: candle_open_out.ljust(col_just3) ) candle_win.setpos(out_line_no, Coinbot::UI.col_fourth) Coinbot::UI.colorize( ui_win: candle_win, color: last_ticker_price_color, style: :bold, string: last_ticker_price_out.rjust(col_just4) ) # ROW 2 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :green, style: :bold, string: 'Candle High:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :green, string: "#{Coinbot.up_arrow} #{fiat_symbol}#{candle_highest_out}".rjust( col_just3_alt, '.' ) ) # ROW 3 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :red, style: :bold, string: 'Candle Low:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :red, string: "#{Coinbot.down_arrow} #{fiat_symbol}#{candle_lowest_out}".rjust( col_just3_alt, '.' ) ) # ROW 4 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :cyan, style: :bold, string: 'Order High:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :cyan, string: "#{Coinbot.up_arrow} #{fiat_symbol}#{order_highest_out}".rjust( col_just3_alt, '.' ) ) # ROW 5 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :blue, style: :bold, string: 'Order Low:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :blue, string: "#{Coinbot.down_arrow} #{fiat_symbol}#{order_lowest_out}".rjust( col_just3_alt, '.' ) ) # ROW 6 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: order_trend_color, style: :bold, string: 'Order Trend:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: order_trend_color, string: order_trend_out.rjust( col_just3_alt, '.' ) ) # ROW 7 out_line_no += 1 Coinbot::UI.line( ui_win: candle_win, out_line_no: out_line_no ) # ROW 8 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :yellow, style: :bold, string: 'SMA 9 | 50 | 180:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :yellow, string: "$#{sma_nine} | $#{sma_fifty} | $#{sma_one_eighty}".rjust( col_just3_alt, '.' ) ) # ROW 9 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :cyan, style: :bold, string: "EMA #{ema_low} | #{ema_high} | 180:" ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :cyan, string: "$#{ema_l_avg} | $#{ema_h_avg} | $#{ema_one_eighty}".rjust( col_just3_alt, '.' ) ) # ROW 10 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :yellow, style: :bold, string: '(E12-E26=M)-(E9->M=Sig)=MACDH:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :yellow, string: "(#{macd_fast_avg} - #{macd_slow_avg} = #{macd}) - (#{signal}) = #{histogram}".rjust( col_just3_alt, '.' ) ) # ROW 11 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :magenta, string: 'RSI:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :magenta, string: rsi.to_s.rjust( col_just3_alt, '.' ) ) # ROW 12 out_line_no += 1 candle_win.setpos(out_line_no, Coinbot::UI.col_first) candle_win.clrtoeol Coinbot::UI.colorize( ui_win: candle_win, color: :blue, style: :bold, string: 'Pie-in-the-Sky bConf | Observed:' ) candle_win.setpos(out_line_no, Coinbot::UI.col_third) Coinbot::UI.colorize( ui_win: candle_win, color: :blue, string: "BUY >>> #{pie_buy_str} SELL >>> #{pie_sell_str}".rjust( col_just3_alt, '.' ) ) candle_win.refresh rescue Interrupt # Exit Gracefully if CTRL+C is Pressed During Session Coinbot.exit_gracefully(which_self: self) rescue StandardError => e raise e end |