# frozen_string_literal: true module Cryptum # Cryptum::API Namespace module API # This Module is used to interact with the Order APIs module Orders public_class_method def self.submit_limit(opts = {}) option_choice = opts[:option_choice] env = opts[:env] price = opts[:price] size = opts[:size] buy_or_sell = opts[:buy_or_sell] event_history = opts[:event_history] bot_conf = opts[:bot_conf] buy_order_id = opts[:buy_order_id] tpm = bot_conf[:target_profit_margin_percent].to_f tpm_cast_as_decimal = tpm / 100 product_id = option_choice.symbol.to_s.gsub('_', '-').upcase this_product = event_history.order_book[:this_product] 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 order_hash = {} order_hash[:type] = 'limit' order_hash[:time_in_force] = 'GTC' if buy_or_sell == :buy order_hash[:time_in_force] = 'GTT' order_hash[:cancel_after] = 'min' end 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_resp = Cryptum::API::Rest.call( option_choice: option_choice, env: env, http_method: :POST, api_call: '/orders', http_body: http_body, base_increment: base_increment ) # Populate Order ID on the Buy # to know what to do on the Sell case buy_or_sell when :buy this_order = event_history.order_book[:order_plan].shift this_order[:buy_order_id] = limit_order_resp[:id] this_order[:price] = price targ_price = price.to_f + (price.to_f * tpm_cast_as_decimal) this_order[:tpm] = format( '%0.2f', tpm ) this_order[:target_price] = format( "%0.#{fiat_smallest_decimal}f", targ_price ) this_order[:size] = size this_order[:color] = :cyan event_history.order_book[:order_history_meta].push(this_order) when :sell sell_order_id = limit_order_resp[:id] event_history.order_book[:order_history_meta].each do |meta| if meta[:buy_order_id] == buy_order_id meta[:sell_order_id] = sell_order_id meta[:color] = :yellow end end end event_history rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history) end # 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] # product_id = option_choice.symbol.to_s.gsub('_', '-').upcase # order_hash = {} # order_hash[:product_id] = product_id # params = order_hash # Cryptum::API::Rest.call( # env: env, # http_method: :DELETE, # api_call: "/orders/#{order_id}", # option_choice: option_choice, # params: params, # order_type: order_type # ) # rescue Interrupt, StandardError => e # Cryptum::Log.append(level: :error, msg: e, which_self: self) # end public_class_method def self.cancel_all_open_orders(opts = {}) env = opts[:env] option_choice = opts[:option_choice] event_notes = opts[:event_notes] product_id = option_choice.symbol.to_s.gsub('_', '-').upcase order_hash = {} order_hash[:product_id] = product_id # http_body = order_hash.to_json params = order_hash canceled_order_id_arr = [] loop do canceled_order_id_arr = Cryptum::API::Rest.call( env: env, http_method: :DELETE, api_call: '/orders', option_choice: option_choice, params: params, event_notes: event_notes ) break if canceled_order_id_arr.empty? end canceled_order_id_arr rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self) end public_class_method def self.gtfo(opts = {}) option_choice = opts[:option_choice] env = opts[:env] event_history = opts[:event_history] # Cancel all open orders cancel_all_open_orders( env: env, option_choice: option_choice ) product_id = option_choice.symbol.to_s.gsub('_', '-').upcase this_product = event_history.order_book[:this_product] 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 # Calculate / Price / Size last_three_prices_arr = [] 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 last_three_prices_arr.push(last_ticker_price) last_three_prices_arr.push(second_to_last_ticker_price) last_three_prices_arr.push(third_to_last_ticker_price) limit_price = last_three_prices_arr.sort[1] price = format( "%0.#{fiat_smallest_decimal}f", limit_price ) crypto_currency = option_choice.symbol.to_s.upcase.split('_').first.to_sym portfolio = event_history.order_book[:portfolio] this_account = portfolio.select do |account| account[:currency] == crypto_currency.to_s end balance = format( "%0.#{crypto_smallest_decimal}f", this_account.first[:balance] ) current_crypto_fiat_value = format( '%0.2f', balance.to_f * price.to_f ) order_hash = {} order_hash[:type] = 'limit' order_hash[:time_in_force] = 'GTT' order_hash[:cancel_after] = 'min' order_hash[:size] = balance order_hash[:price] = price order_hash[:side] = :sell order_hash[:product_id] = product_id http_body = order_hash.to_json limit_order_resp = Cryptum::API::Rest.call( option_choice: option_choice, env: env, http_method: :POST, api_call: '/orders', http_body: http_body, base_increment: base_increment ) # Populate Order ID on the Buy # to know what to do on the Sell this_order = {} this_order[:plan_no] = '0.0' this_order[:fiat_available] = '0.00' this_order[:risk_alloc] = current_crypto_fiat_value this_order[:allocation_decimal] = '1.0' this_order[:allocation_percent] = '100.0' this_order[:invest] = current_crypto_fiat_value this_order[:return] = current_crypto_fiat_value this_order[:profit] = '0.0' this_order[:buy_order_id] = 'N/A' this_order[:price] = price this_order[:tpm] = '0.00' this_order[:target_price] = current_crypto_fiat_value this_order[:size] = balance this_order[:color] = :magenta this_order[:sell_order_id] = limit_order_resp[:id] event_history.order_book[:order_history_meta].push(this_order) event_history rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history) end public_class_method def self.help puts "USAGE: event_history = #{self}.submit_limit( env: 'required - Coinbase::Option::Environment.get Object' ) canceled_order_id_arr = #{self}.cancel_all_open_orders( env: 'required - Coinbase::Option::Environment.get Object' ) event_history = #{self}.gtfo( env: 'required - Coinbase::Option::Environment.get Object' ) " end end end end