Module: Cryptum::API::Orders

Defined in:
lib/cryptum/api/orders.rb

Overview

This Module is used to interact with the Order APIs

Class Method Summary collapse

Class Method Details

.cancel_all_open_orders(opts = {}) ⇒ Object

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



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
# File 'lib/cryptum/api/orders.rb', line 112

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

.gtfo(opts = {}) ⇒ Object



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
# File 'lib/cryptum/api/orders.rb', line 143

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]
   = portfolio.select do ||
    [:currency] == crypto_currency.to_s
  end
  balance = format(
    "%0.#{crypto_smallest_decimal}f",
    .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

.helpObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/cryptum/api/orders.rb', line 239

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

.submit_limit(opts = {}) ⇒ Object



8
9
10
11
12
13
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
# File 'lib/cryptum/api/orders.rb', line 8

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