Module: Coinbot::API
- Defined in:
- lib/coinbot/api.rb
Overview
This plugin is used to interact withbtje Coinbase REST API
Class Method Summary collapse
- .cancel_all_open_orders(opts = {}) ⇒ Object
- .cancel_open_order(opts = {}) ⇒ Object
-
.generate_signature(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::API.generate_signature( ).
- .get_candle_history(opts = {}) ⇒ Object
- .get_exchange_rates(opts = {}) ⇒ Object
- .get_fees(opts = {}) ⇒ Object
- .get_order_history(opts = {}) ⇒ Object
- .get_portfolio(opts = {}) ⇒ Object
- .get_products(opts = {}) ⇒ Object
-
.help ⇒ Object
Display Usage for this Module.
- .submit_limit_order(opts = {}) ⇒ Object
Class Method Details
.cancel_all_open_orders(opts = {}) ⇒ Object
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 |
# File 'lib/coinbot/api.rb', line 356 public_class_method def self.cancel_all_open_orders(opts = {}) env = opts[:env] option_choice = opts[:option_choice] order_type = opts[:order_type] event_notes = opts[:event_notes] product_id = option_choice.symbol.to_s.gsub('_', '-').upcase order_hash = {} order_hash[:product_id] = product_id params = order_hash rest_api_call( env: env, http_method: :DELETE, api_call: '/orders', option_choice: option_choice, params: params, order_type: order_type, event_notes: event_notes ) rescue StandardError => e raise e end |
.cancel_open_order(opts = {}) ⇒ Object
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 |
# File 'lib/coinbot/api.rb', line 328 public_class_method def self.cancel_open_order(opts = {}) env = opts[:env] option_choice = opts[:option_choice] order_id = opts[:order_id] order_type = opts[:order_type] event_notes = opts[:event_notes] return unless order_id product_id = option_choice.symbol.to_s.gsub('_', '-').upcase # order_hash = {} # order_hash[:product_id] = product_id # params = order_hash # params: params, rest_api_call( env: env, http_method: :DELETE, api_call: "/orders/#{order_id}", option_choice: option_choice, order_type: order_type ) rescue StandardError => e raise e end |
.generate_signature(opts = {}) ⇒ Object
- Supported Method Parameters
-
Coinbot::API.generate_signature( )
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 |
# File 'lib/coinbot/api.rb', line 16 public_class_method def self.generate_signature(opts = {}) api_secret = opts[:api_secret] http_method = if opts[:http_method].nil? :GET else opts[:http_method].to_s.upcase.scrub.strip.chomp.to_sym end api_call = opts[:api_call].to_s.scrub.strip.chomp api_call = '/users/self/verify' if opts[:api_call].nil? if opts[:params].nil? path = api_call else uri = Addressable::URI.new uri.query_values = opts[:params] params = uri.query path = "#{api_call}?#{params}" end http_body = opts[:http_body].to_s.scrub.strip.chomp = Time.now.utc.to_i.to_s api_signature = Base64.strict_encode64( OpenSSL::HMAC.digest( 'sha256', Base64.strict_decode64(api_secret), "#{}#{http_method}#{path}#{http_body}" ) ) if http_body == '' api_signature = Base64.strict_encode64( OpenSSL::HMAC.digest( 'sha256', Base64.strict_decode64(api_secret), "#{}#{http_method}#{path}" ) ) end api_signature_response = {} api_signature_response[:api_timestamp] = api_signature_response[:api_signature] = api_signature api_signature_response rescue StandardError => e raise e end |
.get_candle_history(opts = {}) ⇒ Object
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/coinbot/api.rb', line 546 public_class_method def self.get_candle_history(opts = {}) option_choice = opts[:option_choice] env = opts[:env] = opts[:start_timestamp].to_s = opts[:end_timestamp].to_s params = {} case option_choice.candle_duration when 0,60,300,900 params[:start] = Time.parse( ).strftime('%Y-%m-%dT%H:%M:00%z') params[:end] = Time.parse( ).strftime('%Y-%m-%dT%H:%M:00%z') when 3_600, 21_600 params[:start] = Time.parse( ).strftime('%Y-%m-%dT%H:00:00%z') params[:end] = Time.parse( ).strftime('%Y-%m-%dT%H:00:00%z') when 86_400 params[:start] = Time.parse( ).strftime('%Y-%m-%dT00:00:00%z') params[:end] = Time.parse( ).strftime('%Y-%m-%dT00:00:00%z') end params[:granularity] = option_choice.candle_duration product_id = option_choice.symbol.to_s.gsub('_', '-').upcase orders_api_call = "/products/#{product_id}/candles" rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_call: orders_api_call, params: params ).reverse rescue StandardError => e raise e end |
.get_exchange_rates(opts = {}) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/coinbot/api.rb', line 407 public_class_method def self.get_exchange_rates(opts = {}) option_choice = opts[:option_choice] env = opts[:env] api_endpoint = 'https://api.coinbase.com/v2' exchange_rates_api_call = '/exchange-rates' # We don't always get fees back from Coinbase... # This is a hack to ensure we do. exchange = {} exchange = rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_endpoint: api_endpoint, api_call: exchange_rates_api_call ) exchange[:data][:rates] rescue StandardError => e raise e end |
.get_fees(opts = {}) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/coinbot/api.rb', line 485 public_class_method def self.get_fees(opts = {}) option_choice = opts[:option_choice] env = opts[:env] fees_api_call = '/fees' # We don't always get fees back from Coinbase... # This is a hack to ensure we do. fees = {} fees = rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_call: fees_api_call ) fees rescue StandardError => e raise e end |
.get_order_history(opts = {}) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/coinbot/api.rb', line 506 public_class_method def self.get_order_history(opts = {}) option_choice = opts[:option_choice] env = opts[:env] product_id = option_choice.symbol.to_s.gsub('_', '-').upcase orders_api_call = '/orders' params = {} params[:product_id] = product_id params[:status] = 'all' # We don't always get order_history back from Coinbase... # This is a hack to ensure we do. order_history = [] order_history = rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_call: orders_api_call, params: params ) # Cast UTC Timestamps as local times order_history.each do |order| order[:created_at] = Time.parse( order[:created_at] ).localtime.to_s if order[:done_at] order[:done_at] = Time.parse( order[:done_at] ).localtime.to_s end end order_history rescue StandardError => e raise e end |
.get_portfolio(opts = {}) ⇒ Object
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 |
# File 'lib/coinbot/api.rb', line 430 public_class_method def self.get_portfolio(opts = {}) option_choice = opts[:option_choice] env = opts[:env] crypto = opts[:crypto] fiat = opts[:fiat] fiat_portfolio_file = opts[:fiat_portfolio_file] event_notes = opts[:event_notes] # Retrieve Exchange Rates exchange_rates = get_exchange_rates( option_choice: option_choice, env: env ) portfolio_complete_arr = [] portfolio_complete_arr = rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_call: '/accounts', event_notes: event_notes ) all_products = portfolio_complete_arr.select do |products| products if products[:balance].to_f.positive? end total_holdings = 0.00 all_products.each do |product| currency = product[:currency].to_sym this_exchange_rate = exchange_rates[currency].to_f total_holdings += product[:balance].to_f / this_exchange_rate end crypto_portfolio = portfolio_complete_arr.select do |product| product if product[:currency] == crypto end fiat_portfolio = portfolio_complete_arr.select do |product| product if product[:currency] == fiat end fiat_portfolio.last[:total_holdings] = format( '%0.8f', total_holdings ) File.open(fiat_portfolio_file, 'w') do |f| f.puts fiat_portfolio.to_json end crypto_portfolio rescue StandardError => e raise e end |
.get_products(opts = {}) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/coinbot/api.rb', line 382 public_class_method def self.get_products(opts = {}) option_choice = opts[:option_choice] env = opts[:env] products = rest_api_call( option_choice: option_choice, env: env, http_method: :GET, api_call: '/products' ) if products.length.positive? supported_products_filter = products.select do |product| product[:id].match?(/USD$/) && product[:status] == 'online' && product[:fx_stablecoin] == false end sorted_products = supported_products_filter.sort_by { |hash| hash[:id] } end sorted_products rescue StandardError => e raise e end |
.help ⇒ Object
Display Usage for this Module
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/coinbot/api.rb', line 596 public_class_method def self.help puts "USAGE: signature = #{self}.generate_signature( api_secret: 'required - Coinbase Pro API Secret', http_method: 'optional - Defaults to :GET', api_call: 'optional - Defaults to /users/self/verify', params: 'optional - HTTP GET Parameters', http_body: 'optional HTTP POST Body' ) profiles = #{self}.get_profiles( env: 'required - Coinbase::Option.get_env Object' ) products = #{self}.get_products( env: 'required - Coinbase::Option.get_env Object' ) portfolio = #{self}.get_portfolio( env: 'required - Coinbase::Option.get_env Object' ) order_history = #{self}.get_order_history( env: 'required - Coinbase::Option.get_env Object' ) " end |
.submit_limit_order(opts = {}) ⇒ Object
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 |
# File 'lib/coinbot/api.rb', line 249 public_class_method def self.submit_limit_order(opts = {}) option_choice = opts[:option_choice] env = opts[:env] price = opts[:price] size = opts[:size] buy_or_sell = opts[:buy_or_sell] order_type = opts[:order_type].to_s.to_sym event_history = opts[:event_history] indicator_status = opts[:indicator_status] product_id = option_choice.symbol.to_s.gsub('_', '-').upcase event_history.last_opened_order_id = event_history.this_opened_order_id order_hash = {} order_hash[:type] = 'limit' order_hash[:time_in_force] = 'GTT' case order_type when :tpm # order_hash[:time_in_force] = 'FOK' cancel_after = 'hour' when :gtfo, :pie # order_hash[:time_in_force] = 'FOK' cancel_after = 'min' end order_hash[:cancel_after] = cancel_after order_hash[:size] = size order_hash[:price] = price order_hash[:side] = buy_or_sell order_hash[:product_id] = product_id http_body = order_hash.to_json limit_order_response_json = rest_api_call( option_choice: option_choice, env: env, http_method: :POST, api_call: '/orders', http_body: http_body, order_type: order_type, event_notes: event_history.event_notes ) # Consume Latest Order History Justification from # Order Book, Update Status of Canceled Pie or TPM orders # and Add New Entry, and Save Back to Order Book order_just_history = event_history.order_book[:order_justification_history] indicator_type_hash = Coinbot::OrderBook.get_populated_indicators( indicator_status: indicator_status ) indicator_type_hash[:id] = limit_order_response_json[:id] indicator_type_hash[:order_action] = limit_order_response_json[:side].to_s.to_sym # TODO: Determine how to tag orders # not submitted by coinbot as manual indicator_type_hash[:order_tag] = order_type indicator_type_hash[:order_request] = order_hash indicator_type_hash[:order_status] = :open indicator_type_hash[:last_update] = Time.now.strftime('%Y-%m-%d %H:%M:%S%z') # Add New Entry to Order History Justification from # Order Book and Save Back to Order Book order_just_history.push(indicator_type_hash) event_history.order_book[:order_justification_history] = order_just_history event_history.this_opened_order_id = limit_order_response_json[:id] event_history.last_order_expires = Time.parse( limit_order_response_json[:expire_time] ).localtime.strftime('%Y-%m-%d %H:%M:%S.%N%z') event_history.order_just_expired = false event_history rescue StandardError => e raise e end |