Module: Coinbot::OrderBook::Generate
- Defined in:
- lib/coinbot/order_book/generate.rb
Class Method Summary collapse
-
.help ⇒ Object
Display Usage for this Module.
-
.new_candle(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook.new_candle( candle_period: 'required - candle period (integer)', begin_time: Time.now, candle_open: 'required - candle open price', candle_buy_tot: 'required - total buy orders', candle_sell_tot: 'required - total sell orders' ).
-
.new_order_book(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook.generate( symbol: 'required - target symbol (e.g. btc-usd)', this_product: 'required - this_product', ).
Class Method Details
.help ⇒ Object
Display Usage for this Module
361 362 363 364 365 366 367 368 |
# File 'lib/coinbot/order_book/generate.rb', line 361 public_class_method def self.help puts "USAGE: order_book = #{self}.new_order_book( symbol: 'required - target symbol (e.g. btc-usd)', this_product: 'required - this_product' ) " end |
.new_candle(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook.new_candle(
candle_period: 'required - candle period (integer)', begin_time: Time.now, candle_open: 'required - candle open price', candle_buy_tot: 'required - total buy orders', candle_sell_tot: 'required - total sell orders'
)
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 |
# File 'lib/coinbot/order_book/generate.rb', line 312 public_class_method def self.new_candle(opts = {}) candle_period = opts[:candle_period] begin_time = opts[:begin_time] candle_open = opts[:candle_open] candle_highest = opts[:candle_highest] candle_lowest = opts[:candle_lowest] candle_close = opts[:candle_close] candle_volume = opts[:candle_volume] candle_buy_tot = opts[:candle_buy_tot] candle_sell_tot = opts[:candle_sell_tot] macd_history = opts[:macd_history] rsi_history = opts[:rsi_history] { period: candle_period, begin_time: begin_time.strftime('%Y-%m-%d %H:%M:%S.%N%z'), candle_open: candle_open, candle_highest: candle_highest, candle_lowest: candle_lowest, candle_close: candle_close, candle_volume: candle_volume, order_highest: 0.00, order_lowest: 0.00, candle_buy_tot: candle_buy_tot, candle_sell_tot: candle_sell_tot, order_status: 'HOLD', weighted_avg_status: 'HOLD', weighted_buy_n_ticker_status: 'HOLD', weighted_sell_n_ticker_status: 'HOLD', sma_nine: '--', sma_fifty: '--', sma_one_eighty: '--', ema_twelve: '--', ema_twenty_six: '--', ema_high_avg: '--', ema_low_avg: '--', ema_one_eighty: '--', sip_color: '--', last_sip_color: '--', third_last_sip_color: '--', macd_history: macd_history, rsi_history: rsi_history } rescue StandardError => e raise e end |
.new_order_book(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::OrderBook.generate(
symbol: 'required - target symbol (e.g. btc-usd)', this_product: 'required - this_product',
)
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 |
# File 'lib/coinbot/order_book/generate.rb', line 18 public_class_method def self.new_order_book(opts = {}) option_choice = opts[:option_choice] env = opts[:env] bot_conf = opts[:bot_conf] indicator_history = opts[:indicator_history] order_book_file = "#{option_choice.repo_root}/order_books/#{option_choice.symbol}.ORDER_BOOK.json" # TODO: Handle File that exists # w/ Zero size...N number of # attempts? Timeout may be better. if File.exist?(order_book_file) && File.size?(order_book_file).to_i.positive? order_book = JSON.parse( File.read(order_book_file), symbolize_names: true ) order_justification_history = order_book[:order_justification_history] else order_justification_history = [] end # Only need to retrieve a product list once / session. products = Coinbot::API.get_products( option_choice: option_choice, env: env ) this_product_arr = products.select do |product| product if product[:id] == option_choice.symbol.to_s.gsub('_', '-').upcase end this_product = this_product_arr.first candle_hist = option_choice.candle_history # raw_slices_arr = [] raw_cb_candle_history_arr = [] max_resp = 300.0 time_range_seconds = option_choice.candle_duration * max_resp request_times = (candle_hist / max_resp).ceil $stdout.clear_screen spinner = TTY::Spinner.new( "[:spinner] Retrieving History (#{candle_hist} Candles)", format: :arrow_pulse, hide_cursor: true ) now_epoch = Time.now.strftime('%s').to_i = Time.at( now_epoch - (time_range_seconds * request_times) ).strftime('%Y-%m-%dT%H:%M:%S.%N%z') start_epoch = Time.parse( ).strftime('%s').to_i (1..request_times).each do |n| spinner.spin if n > 1 = Time.at( start_epoch + time_range_seconds ).strftime('%Y-%m-%dT%H:%M:%S.%N%z') end start_epoch = Time.parse( ).strftime('%s').to_i = Time.at( start_epoch + time_range_seconds ).strftime('%Y-%m-%dT%H:%M:%S.%N%z') # Append to existing array (not push) raw_cb_candle_history_arr += Coinbot::API.get_candle_history( env: env, option_choice: option_choice, start_timestamp: , end_timestamp: ) # Avoid Triggering Rate-Limiting Controls sleep 0.6 end # raw_cb_candle_history_arr = [] # raw_slices_arr.each do |request_slice| # request_slice.each do |candle_arr| # raw_cb_candle_history_arr.push(candle_arr) # end # end # Good Debugging # raw_cb_candle_history_arr.each do |candle| # print "#{Time.at(candle.first)}=>" # puts candle.inspect # end # puts raw_cb_candle_history_arr.length # exit # Determine if time between each array # element is == to the respective candle_duration cb_candle_history_arr = [] if raw_cb_candle_history_arr.length == candle_hist cb_candle_history_arr = raw_cb_candle_history_arr else # TODO: Need to ensure most recent candle is one tick ago. previous_epoch_time = raw_cb_candle_history_arr.first.first raw_cb_candle_history_arr.each_with_index do |candle_arr, index| this_epoch_time = candle_arr.first epoch_diff = this_epoch_time - previous_epoch_time if epoch_diff > option_choice.candle_duration more_ticks_needed = (epoch_diff / option_choice.candle_duration) - 1 more_ticks_needed.downto(1).each do |count| missing_candle_arr = [] time = this_epoch_time - (option_choice.candle_duration * count) last_element = raw_cb_candle_history_arr[index - 1] last_element = raw_cb_candle_history_arr.first if index.zero? low = high = open = close = last_element[4] volume = 0.0 missing_candle_arr.push(time) missing_candle_arr.push(low) missing_candle_arr.push(high) missing_candle_arr.push(open) missing_candle_arr.push(close) missing_candle_arr.push(volume) missing_candle_arr.push('INJ') cb_candle_history_arr.push(missing_candle_arr) end end cb_candle_history_arr.push(candle_arr) previous_epoch_time = this_epoch_time end end candle_hist_lookback = candle_hist * -1 # Good Debugging # cb_candle_history_arr[candle_hist_lookback..].each do |candle| # print "#{Time.at(candle.first)}=>" # puts candle.inspect # end # puts cb_candle_history_arr[candle_hist_lookback..].length # exit order_book = { path: order_book_file, symbol: option_choice.symbol, session_buy_tot: 0, session_sell_tot: 0, open_24h: 0.00, high_24h: 0.00, low_24h: 0.00, volume_24h: 0.00, ticker_price: cb_candle_history_arr.first[4], ticker_price_second_to_last: 0.00, ticker_price_third_to_last: 0.00, highest_pie_in_sky_buy_percent: 0.00, highest_pie_in_sky_sell_percent: 0.00, sequence: -1, this_product: this_product, portfolio: [], fiat_portfolio: [], order_history: [], fees: [], order_justification_history: order_justification_history, candles: [] } candle_period = 1 candles = order_book[:candles] ema_low = bot_conf[:exponential_moving_avg][:low].to_i ema_high = bot_conf[:exponential_moving_avg][:high].to_i cb_candle_history_arr[candle_hist_lookback..].each do |candle_arr| coinbase_time = Time.at(candle_arr.first) candle_lowest = candle_arr[1].to_s candle_highest = candle_arr[2].to_s candle_open = candle_arr[3].to_s last_ticker_price = candle_close = candle_arr[4].to_s order_book[:ticker_price] = last_ticker_price candle_volume = candle_arr.last.to_s if candle_period == 1 candles.push( new_candle( candle_period: candle_period, begin_time: coinbase_time, candle_open: candle_open, candle_highest: candle_highest, candle_lowest: candle_lowest, candle_close: candle_close, candle_volume: candle_volume, candle_buy_tot: 0, candle_sell_tot: 0, macd_history: [], rsi_history: [] ) ) end if candle_period > 1 candles.push( new_candle( candle_period: candle_period, begin_time: coinbase_time, candle_open: candle_open, candle_highest: candle_highest, candle_lowest: candle_lowest, candle_close: candle_close, candle_volume: candle_volume, candle_buy_tot: 0, candle_sell_tot: 0, macd_history: candles.last[:macd_history], rsi_history: candles.last[:rsi_history] ) ) end candles = Coinbot::OrderBook::Synchronize.candles( last_ticker_price: last_ticker_price, this_product: this_product, candles: candles, indicator_history: indicator_history, bot_conf: bot_conf ) candle_period += 1 end spinner.success("[COMPLETE]") # Order Book Retention ---------------------------------------# # Defaults to 6 hours if candle_duration == 1 minute candle_retention = ema_high candle_retention = 180 if ema_high < 180 candle_retention = ema_high if ema_high > 360 if candles.length >= candle_retention # [retain_history..] << Not a typo - equivalent to # [retain_record..-1] retain_record = candle_retention * -1 candle_history_arr = candles[retain_record..] candles = candle_history_arr order_book[:candles] = candles end # Instantiate Event History attr_accessible # Object to Keep Track of Everything as Events # are Parsed. event_history = Coinbot::Event::History.new( order_book: order_book ) # Wait until the top of the candle duration to begin. event_history = Coinbot.derive_session_init( option_choice: option_choice, env: env, event_history: event_history ) last_ticker_price = candle_open = candles.last[:candle_close] candles.push( new_candle( candle_period: candle_period, begin_time: Time.now, candle_open: candle_open, candle_highest: 0.00, candle_lowest: 0.00, candle_close: 0.00, candle_volume: 0.00, candle_buy_tot: 0, candle_sell_tot: 0, macd_history: candles.last[:macd_history], rsi_history: candles.last[:rsi_history] ) ) # Write order_book to file at session initiation order_book_file = order_book[:path] File.open(order_book_file, 'w') do |f| f.puts order_book.to_json end event_history.order_book = order_book event_history rescue StandardError => e raise e end |