cryptum 0.0.380 → 0.0.381

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c3982c00e3f216242d2e27886cd5896804f3605d540394c6961d0d5c1bc38db
4
- data.tar.gz: d346e20098bcb5488d4a0a5067b8004ddf26674e8d181fa24676d10a80dda370
3
+ metadata.gz: 866bb0c564c84fd84b9795396ffabf73483cea8c71171e3de935545c9a5a7736
4
+ data.tar.gz: 060b3793c66b3a31d7747d5677de5f7b346d14ac00714357188239080a19bd5d
5
5
  SHA512:
6
- metadata.gz: 6b4b4cbb7db373840040049d056bcfb58fa0ead62230cefb8953d4573b29043f343f3eee2ac23166806a212de26d2a4aa59ea39cc94959c41e315ac2df2280e7
7
- data.tar.gz: 18951472ce501e7f80332fd531e9f7dc70483af3d11b0efce8ee117d86b47dbb8848c52e0c6fc27e5fe13ced953c8ec4fb5487333368032c395776160d1bddcd
6
+ metadata.gz: 07c342bb8da387ff8fad07303102860758b5be1334d6558732255a2f743a607bb5a881968f1da1506bda887819d82cd6d5a473b19380f9641dac3b7d43c2ae81
7
+ data.tar.gz: 928cd958349f76561e312529f3787d708924031ca6e73cec4c3314d59b639479d3c1997d70c6227e27360c66ceee038a5260ab61c3b4104f98fea7f764a50b1d
data/.rubocop_todo.yml CHANGED
@@ -1,16 +1,11 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2023-03-30 21:02:59 UTC using RuboCop version 1.48.1.
3
+ # on 2023-03-30 22:47:49 UTC using RuboCop version 1.48.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 4
10
- Lint/UselessAssignment:
11
- Exclude:
12
- - 'lib/cryptum/api.rb'
13
-
14
9
  # Offense count: 1
15
10
  # Configuration parameters: CountComments, CountAsOne.
16
11
  Metrics/ClassLength:
@@ -25,13 +20,7 @@ Style/ConditionalAssignment:
25
20
  - 'lib/cryptum/event/pane.rb'
26
21
  - 'lib/cryptum/ui/order_execute_details.rb'
27
22
 
28
- # Offense count: 23
23
+ # Offense count: 24
29
24
  # Configuration parameters: AllowedConstants.
30
25
  Style/Documentation:
31
26
  Enabled: false
32
-
33
- # Offense count: 1
34
- # This cop supports safe autocorrection (--autocorrect).
35
- Style/RedundantAssignment:
36
- Exclude:
37
- - 'lib/cryptum/api.rb'
data/bin/cryptum CHANGED
@@ -15,7 +15,7 @@ begin
15
15
 
16
16
  # Dump out supported products if --list-products flag is passed and exit
17
17
  if option_choice.list_products
18
- Cryptum::API.list_products_and_exit(
18
+ Cryptum::API::Products.list_and_exit(
19
19
  option_choice: option_choice,
20
20
  env: env
21
21
  )
data/bin/cryptum-forecast CHANGED
@@ -118,7 +118,7 @@ begin
118
118
  # Read in Bot Conf Values
119
119
  bot_conf = Cryptum::BotConf.read(option_choice: option_choice)
120
120
 
121
- products = Cryptum::API.get_products(
121
+ products = Cryptum::API::Products.get(
122
122
  option_choice: option_choice,
123
123
  env: env
124
124
  )
@@ -127,7 +127,7 @@ begin
127
127
  fiat = products.last[:quote_currency]
128
128
  fiat_portfolio_file = "#{option_choice.session_root}/order_books/#{fiat}_PORTFOLIO.json"
129
129
 
130
- # portfolio = Cryptum::API.get_portfolio(
130
+ # portfolio = Cryptum::API::Portfolio.get(
131
131
  # option_choice: option_choice,
132
132
  # env: env,
133
133
  # crypto: crypto,
@@ -149,7 +149,7 @@ begin
149
149
  gross_tpm = bot_conf[:target_profit_margin_percent].to_f / 100
150
150
  gross_tpm = option_choice.tpm.to_f if option_choice.tpm.to_f.positive?
151
151
 
152
- fees = Cryptum::API.get_fees(
152
+ fees = Cryptum::API::Fees.get(
153
153
  option_choice: option_choice,
154
154
  env: env
155
155
  )
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Cryptum
6
+ # This module is used to Interact with the APIs
7
+ module API
8
+ # Module specifically related to orders history retrieval.
9
+ module ExchangeRates
10
+ # Obtain latest order history
11
+ public_class_method def self.get(opts = {})
12
+ option_choice = opts[:option_choice]
13
+ env = opts[:env]
14
+
15
+ api_endpoint = 'https://api.coinbase.com/v2'
16
+ exchange_rates_api_call = '/exchange-rates'
17
+
18
+ # We don't always get fees back from Coinbase...
19
+ # This is a hack to ensure we do.
20
+ Cryptum::API::Rest.call(
21
+ option_choice: option_choice,
22
+ env: env,
23
+ http_method: :GET,
24
+ api_endpoint: api_endpoint,
25
+ api_call: exchange_rates_api_call
26
+ )
27
+ rescue StandardError => e
28
+ raise e
29
+ end
30
+
31
+ # Display Usage for this Module
32
+ public_class_method def self.help
33
+ puts "USAGE:
34
+ fees = #{self}.get(
35
+ env: 'required - Coinbase::Option::Environment.get Object',
36
+ option_choice: 'required - Coinbase::Option::Choice Object'
37
+ )
38
+ "
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptum
4
+ # This module is used to Interact with the APIs
5
+ module API
6
+ # Module specifically related to orders history retrieval.
7
+ module Fees
8
+ # Obtain latest order history
9
+ public_class_method def self.get(opts = {})
10
+ option_choice = opts[:option_choice]
11
+ env = opts[:env]
12
+
13
+ fees_api_call = '/fees'
14
+
15
+ Cryptum::API::Rest.call(
16
+ option_choice: option_choice,
17
+ env: env,
18
+ http_method: :GET,
19
+ api_call: fees_api_call
20
+ )
21
+ rescue StandardError => e
22
+ raise e
23
+ end
24
+
25
+ # Display Usage for this Module
26
+ public_class_method def self.help
27
+ puts "USAGE:
28
+ fees = #{self}.get(
29
+ env: 'required - Coinbase::Option::Environment.get Object',
30
+ option_choice: 'required - Coinbase::Option::Choice Object'
31
+ )
32
+ "
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptum
4
+ # This module is used to Interact with the APIs
5
+ module API
6
+ # Module specifically related to orders history retrieval.
7
+ module OrderHistory
8
+ # Obtain latest order history
9
+ public_class_method def self.get(opts = {})
10
+ option_choice = opts[:option_choice]
11
+ env = opts[:env]
12
+
13
+ product_id = option_choice.symbol.to_s.gsub('_', '-').upcase
14
+
15
+ orders_api_call = '/orders'
16
+ params = {}
17
+ params[:product_id] = product_id
18
+ params[:status] = 'all'
19
+
20
+ order_history = Cryptum::API::Rest.call(
21
+ option_choice: option_choice,
22
+ env: env,
23
+ http_method: :GET,
24
+ api_call: orders_api_call,
25
+ params: params
26
+ )
27
+
28
+ # Cast UTC Timestamps as local times
29
+ order_history.each do |order|
30
+ order[:created_at] = Time.parse(
31
+ order[:created_at]
32
+ ).localtime.to_s
33
+
34
+ next unless order[:done_at]
35
+
36
+ order[:done_at] = Time.parse(
37
+ order[:done_at]
38
+ ).localtime.to_s
39
+ end
40
+
41
+ order_history
42
+ rescue StandardError => e
43
+ raise e
44
+ end
45
+
46
+ # Display Usage for this Module
47
+ public_class_method def self.help
48
+ puts "USAGE:
49
+ order_history = #{self}.get_order_history(
50
+ env: 'required - Coinbase::Option::Environment.get Object'
51
+ )
52
+ "
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,253 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'addressable'
4
+ require 'base64'
5
+ require 'json'
6
+ require 'openssl'
7
+ require 'rest-client'
8
+
9
+ module Cryptum
10
+ # This plugin is used to interact withbtje Coinbase REST API
11
+ module API
12
+ module Orders
13
+ public_class_method def self.submit_limit_order(opts = {})
14
+ option_choice = opts[:option_choice]
15
+ env = opts[:env]
16
+ price = opts[:price]
17
+ size = opts[:size]
18
+ buy_or_sell = opts[:buy_or_sell]
19
+ event_history = opts[:event_history]
20
+ bot_conf = opts[:bot_conf]
21
+ buy_order_id = opts[:buy_order_id]
22
+
23
+ tpm = bot_conf[:target_profit_margin_percent].to_f
24
+ tpm_cast_as_decimal = tpm / 100
25
+
26
+ product_id = option_choice.symbol.to_s.gsub('_', '-').upcase
27
+
28
+ this_product = event_history.order_book[:this_product]
29
+ base_increment = this_product[:base_increment]
30
+ quote_increment = this_product[:quote_increment]
31
+ # crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length
32
+ fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length
33
+
34
+ order_hash = {}
35
+
36
+ order_hash[:type] = 'limit'
37
+ order_hash[:time_in_force] = 'GTC'
38
+
39
+ if buy_or_sell == :buy
40
+ order_hash[:time_in_force] = 'GTT'
41
+ order_hash[:cancel_after] = 'min'
42
+ end
43
+
44
+ order_hash[:size] = size
45
+ order_hash[:price] = price
46
+ order_hash[:side] = buy_or_sell
47
+ order_hash[:product_id] = product_id
48
+
49
+ http_body = order_hash.to_json
50
+
51
+ limit_order_resp = Cryptum::API::Rest.call(
52
+ option_choice: option_choice,
53
+ env: env,
54
+ http_method: :POST,
55
+ api_call: '/orders',
56
+ http_body: http_body,
57
+ base_increment: base_increment
58
+ )
59
+
60
+ # Populate Order ID on the Buy
61
+ # to know what to do on the Sell
62
+ case buy_or_sell
63
+ when :buy
64
+ this_order = event_history.order_book[:order_plan].shift
65
+ this_order[:buy_order_id] = limit_order_resp[:id]
66
+ this_order[:price] = price
67
+ targ_price = price.to_f + (price.to_f * tpm_cast_as_decimal)
68
+ this_order[:tpm] = format(
69
+ '%0.2f',
70
+ tpm
71
+ )
72
+ this_order[:target_price] = format(
73
+ "%0.#{fiat_smallest_decimal}f",
74
+ targ_price
75
+ )
76
+
77
+ this_order[:size] = size
78
+
79
+ this_order[:color] = :cyan
80
+ event_history.order_book[:order_history_meta].push(this_order)
81
+ when :sell
82
+ sell_order_id = limit_order_resp[:id]
83
+ event_history.order_book[:order_history_meta].each do |meta|
84
+ if meta[:buy_order_id] == buy_order_id
85
+ meta[:sell_order_id] = sell_order_id
86
+ meta[:color] = :yellow
87
+ end
88
+ end
89
+ end
90
+
91
+ event_history
92
+ rescue StandardError => e
93
+ raise e
94
+ end
95
+
96
+ # public_class_method def self.cancel_open_order(opts = {})
97
+ # env = opts[:env]
98
+ # option_choice = opts[:option_choice]
99
+ # order_id = opts[:order_id]
100
+ # order_type = opts[:order_type]
101
+ # product_id = option_choice.symbol.to_s.gsub('_', '-').upcase
102
+ # order_hash = {}
103
+ # order_hash[:product_id] = product_id
104
+ # params = order_hash
105
+ # Cryptum::API::Rest.call(
106
+ # env: env,
107
+ # http_method: :DELETE,
108
+ # api_call: "/orders/#{order_id}",
109
+ # option_choice: option_choice,
110
+ # params: params,
111
+ # order_type: order_type
112
+ # )
113
+ # rescue StandardError => e
114
+ # raise e
115
+ # end
116
+
117
+ public_class_method def self.cancel_all_open_orders(opts = {})
118
+ env = opts[:env]
119
+ option_choice = opts[:option_choice]
120
+ event_notes = opts[:event_notes]
121
+
122
+ product_id = option_choice.symbol.to_s.gsub('_', '-').upcase
123
+
124
+ order_hash = {}
125
+ order_hash[:product_id] = product_id
126
+
127
+ # http_body = order_hash.to_json
128
+ params = order_hash
129
+
130
+ canceled_order_id_arr = []
131
+ loop do
132
+ canceled_order_id_arr = Cryptum::API::Rest.call(
133
+ env: env,
134
+ http_method: :DELETE,
135
+ api_call: '/orders',
136
+ option_choice: option_choice,
137
+ params: params,
138
+ event_notes: event_notes
139
+ )
140
+
141
+ break if canceled_order_id_arr.empty?
142
+ end
143
+ canceled_order_id_arr
144
+ rescue StandardError => e
145
+ raise e
146
+ end
147
+
148
+ public_class_method def self.gtfo(opts = {})
149
+ option_choice = opts[:option_choice]
150
+ env = opts[:env]
151
+ event_history = opts[:event_history]
152
+
153
+ # Cancel all open orders
154
+ cancel_all_open_orders(
155
+ env: env,
156
+ option_choice: option_choice
157
+ )
158
+
159
+ product_id = option_choice.symbol.to_s.gsub('_', '-').upcase
160
+
161
+ this_product = event_history.order_book[:this_product]
162
+ base_increment = this_product[:base_increment]
163
+ quote_increment = this_product[:quote_increment]
164
+ crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length
165
+ fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length
166
+
167
+ # TODO: Calculate / Price / Size
168
+ last_three_prices_arr = []
169
+ last_ticker_price = event_history.order_book[:ticker_price].to_f
170
+ second_to_last_ticker_price = event_history.order_book[:ticker_price_second_to_last].to_f
171
+ third_to_last_ticker_price = event_history.order_book[:ticker_price_third_to_last].to_f
172
+ last_three_prices_arr.push(last_ticker_price)
173
+ last_three_prices_arr.push(second_to_last_ticker_price)
174
+ last_three_prices_arr.push(third_to_last_ticker_price)
175
+ limit_price = last_three_prices_arr.sort[1]
176
+ price = format(
177
+ "%0.#{fiat_smallest_decimal}f",
178
+ limit_price
179
+ )
180
+
181
+ crypto_currency = option_choice.symbol.to_s.upcase.split('_').first.to_sym
182
+ portfolio = event_history.order_book[:portfolio]
183
+ this_account = portfolio.select do |account|
184
+ account[:currency] == crypto_currency.to_s
185
+ end
186
+ balance = format(
187
+ "%0.#{crypto_smallest_decimal}f",
188
+ this_account.first[:balance]
189
+ )
190
+
191
+ current_crypto_fiat_value = format(
192
+ '%0.2f',
193
+ balance.to_f * price.to_f
194
+ )
195
+
196
+ order_hash = {}
197
+
198
+ order_hash[:type] = 'limit'
199
+ order_hash[:time_in_force] = 'GTT'
200
+ order_hash[:cancel_after] = 'min'
201
+
202
+ order_hash[:size] = balance
203
+ order_hash[:price] = price
204
+ order_hash[:side] = :sell
205
+ order_hash[:product_id] = product_id
206
+
207
+ http_body = order_hash.to_json
208
+
209
+ limit_order_resp = Cryptum::API::Rest.call(
210
+ option_choice: option_choice,
211
+ env: env,
212
+ http_method: :POST,
213
+ api_call: '/orders',
214
+ http_body: http_body,
215
+ base_increment: base_increment
216
+ )
217
+
218
+ # Populate Order ID on the Buy
219
+ # to know what to do on the Sell
220
+ this_order = {}
221
+ this_order[:plan_no] = '0.0'
222
+ this_order[:fiat_available] = '0.00'
223
+ this_order[:risk_alloc] = current_crypto_fiat_value
224
+ this_order[:allocation_decimal] = '1.0'
225
+ this_order[:allocation_percent] = '100.0'
226
+ this_order[:invest] = current_crypto_fiat_value
227
+ this_order[:return] = current_crypto_fiat_value
228
+ this_order[:profit] = '0.0'
229
+ this_order[:buy_order_id] = 'N/A'
230
+ this_order[:price] = price
231
+ this_order[:tpm] = '0.00'
232
+ this_order[:target_price] = current_crypto_fiat_value
233
+ this_order[:size] = balance
234
+ this_order[:color] = :magenta
235
+ this_order[:sell_order_id] = limit_order_resp[:id]
236
+
237
+ event_history.order_book[:order_history_meta].push(this_order)
238
+
239
+ event_history
240
+ rescue StandardError => e
241
+ raise e
242
+ end
243
+
244
+ public_class_method def self.help
245
+ puts "USAGE:
246
+ profiles = #{self}.get_profiles(
247
+ env: 'required - Coinbase::Option::Environment.get Object'
248
+ )
249
+ "
250
+ end
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Cryptum
6
+ # This module is used to Interact with the APIs
7
+ module API
8
+ # Module specifically related to orders history retrieval.
9
+ module Portfolio
10
+ # Obtain latest order history
11
+ public_class_method def self.get(opts = {})
12
+ option_choice = opts[:option_choice]
13
+ env = opts[:env]
14
+ crypto = opts[:crypto]
15
+ fiat = opts[:fiat]
16
+ fiat_portfolio_file = opts[:fiat_portfolio_file]
17
+ event_notes = opts[:event_notes]
18
+
19
+ # Retrieve Exchange Rates
20
+ exchange_rates_resp = Cryptum::API::ExchangeRates.get(
21
+ option_choice: option_choice,
22
+ env: env
23
+ )
24
+ exchange_rates = exchange_rates_resp[:data][:rates]
25
+
26
+ portfolio_complete_arr = Cryptum::API::Rest.call(
27
+ option_choice: option_choice,
28
+ env: env,
29
+ http_method: :GET,
30
+ api_call: '/accounts',
31
+ event_notes: event_notes
32
+ )
33
+ portfolio_complete_arr ||= []
34
+
35
+ all_products = portfolio_complete_arr.select do |products|
36
+ products if products[:balance].to_f.positive?
37
+ end
38
+
39
+ total_holdings = 0.00
40
+ all_products.each do |product|
41
+ currency = product[:currency].to_sym
42
+ this_exchange_rate = exchange_rates[currency].to_f
43
+ total_holdings += product[:balance].to_f / this_exchange_rate
44
+ end
45
+
46
+ crypto_portfolio = portfolio_complete_arr.select do |product|
47
+ product if product[:currency] == crypto
48
+ end
49
+
50
+ fiat_portfolio = portfolio_complete_arr.select do |product|
51
+ product if product[:currency] == fiat
52
+ end
53
+ fiat_portfolio.last[:total_holdings] = format(
54
+ '%0.8f',
55
+ total_holdings
56
+ )
57
+
58
+ File.write(
59
+ fiat_portfolio_file,
60
+ JSON.pretty_generate(fiat_portfolio)
61
+ )
62
+
63
+ crypto_portfolio
64
+ rescue StandardError => e
65
+ raise e
66
+ end
67
+
68
+ # Display Usage for this Module
69
+ public_class_method def self.help
70
+ puts "USAGE:
71
+ fees = #{self}.get(
72
+ env: 'required - Coinbase::Option::Environment.get Object',
73
+ option_choice: 'required - Coinbase::Option::Choice Object'
74
+ )
75
+ "
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptum
4
+ # This module is used to Interact with the APIs
5
+ module API
6
+ # Module specifically related to orders history retrieval.
7
+ module Products
8
+ # Obtain latest order history
9
+ public_class_method def self.get(opts = {})
10
+ option_choice = opts[:option_choice]
11
+ env = opts[:env]
12
+
13
+ products = Cryptum::API::Rest.call(
14
+ option_choice: option_choice,
15
+ env: env,
16
+ http_method: :GET,
17
+ api_call: '/products'
18
+ )
19
+
20
+ if products.length.positive?
21
+ supported_products_filter = products.select do |product|
22
+ product[:id].match?(/USD$/) &&
23
+ product[:status] == 'online' &&
24
+ product[:fx_stablecoin] == false
25
+ end
26
+ sorted_products = supported_products_filter.sort_by { |hash| hash[:id] }
27
+ end
28
+
29
+ sorted_products
30
+ rescue StandardError => e
31
+ raise e
32
+ end
33
+
34
+ # List Supported Cryptum Products and Exit
35
+ public_class_method def self.list_and_exit(opts = {})
36
+ option_choice = opts[:option_choice]
37
+ env = opts[:env]
38
+
39
+ puts "\n#{option_choice.driver_name} Supports the Following Products:"
40
+ products = get(
41
+ option_choice: option_choice,
42
+ env: env
43
+ )
44
+
45
+ products.map do |product|
46
+ puts product[:id].downcase
47
+ end
48
+
49
+ exit 0
50
+ rescue StandardError => e
51
+ raise e
52
+ end
53
+
54
+ # Display Usage for this Module
55
+ public_class_method def self.help
56
+ puts "USAGE:
57
+ fees = #{self}.get(
58
+ env: 'required - Coinbase::Option::Environment.get Object',
59
+ option_choice: 'required - Coinbase::Option::Choice Object'
60
+ )
61
+ "
62
+ end
63
+ end
64
+ end
65
+ end