cryptum 0.0.358 → 0.0.360

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +16 -0
  3. data/.gitignore +30 -0
  4. data/.rspec +3 -0
  5. data/.rspec_status +0 -0
  6. data/.rubocop.yml +31 -0
  7. data/.rubocop_todo.yml +36 -0
  8. data/.ruby-gemset +1 -0
  9. data/.ruby-version +1 -0
  10. data/CODE_OF_CONDUCT.md +84 -0
  11. data/Gemfile +38 -0
  12. data/LICENSE +674 -0
  13. data/README.md +87 -0
  14. data/Rakefile +19 -0
  15. data/bin/cryptum +73 -0
  16. data/bin/cryptum-forecast +200 -0
  17. data/bin/cryptum-repl +73 -0
  18. data/bin/cryptum_autoinc_version +38 -0
  19. data/build_cryptum_gem.sh +58 -0
  20. data/cryptum.gemspec +52 -0
  21. data/cryptum_container.sh +1 -0
  22. data/docker/cryptum.json +60 -0
  23. data/docker/cryptum_container.sh +59 -0
  24. data/docker/packer_secrets.json.EXAMPLE +7 -0
  25. data/docker/provisioners/cryptum.sh +11 -0
  26. data/docker/provisioners/docker_bashrc.sh +2 -0
  27. data/docker/provisioners/docker_rvm.sh +22 -0
  28. data/docker/provisioners/init_image.sh +28 -0
  29. data/docker/provisioners/post_install.sh +6 -0
  30. data/docker/provisioners/ruby.sh +16 -0
  31. data/docker/provisioners/upload_globals.sh +49 -0
  32. data/etc/bot_confs/.gitkeep +0 -0
  33. data/etc/bot_confs/BOT_CONF.TEMPLATE +10 -0
  34. data/etc/coinbase_pro.yaml.EXAMPLE +8 -0
  35. data/etc/open_ai.yaml.EXAMPLE +1 -0
  36. data/git_commit.sh +22 -0
  37. data/lib/cryptum/api.rb +688 -0
  38. data/lib/cryptum/bot_conf.rb +197 -0
  39. data/lib/cryptum/event/bot_conf.rb +34 -0
  40. data/lib/cryptum/event/buy.rb +145 -0
  41. data/lib/cryptum/event/cancel.rb +35 -0
  42. data/lib/cryptum/event/exit.rb +35 -0
  43. data/lib/cryptum/event/gtfo.rb +36 -0
  44. data/lib/cryptum/event/history.rb +108 -0
  45. data/lib/cryptum/event/key_press.rb +64 -0
  46. data/lib/cryptum/event/order_book.rb +34 -0
  47. data/lib/cryptum/event/pane.rb +65 -0
  48. data/lib/cryptum/event/parse.rb +181 -0
  49. data/lib/cryptum/event/scroll.rb +200 -0
  50. data/lib/cryptum/event/sell.rb +124 -0
  51. data/lib/cryptum/event.rb +27 -0
  52. data/lib/cryptum/log.rb +34 -0
  53. data/lib/cryptum/matrix.rb +181 -0
  54. data/lib/cryptum/open_ai.rb +156 -0
  55. data/lib/cryptum/option/choice.rb +28 -0
  56. data/lib/cryptum/option.rb +206 -0
  57. data/lib/cryptum/order_book/generate.rb +114 -0
  58. data/lib/cryptum/order_book/indicator.rb +15 -0
  59. data/lib/cryptum/order_book/market_trend.rb +137 -0
  60. data/lib/cryptum/order_book/profit_margin.rb +55 -0
  61. data/lib/cryptum/order_book.rb +19 -0
  62. data/lib/cryptum/portfolio/balance.rb +123 -0
  63. data/lib/cryptum/portfolio.rb +15 -0
  64. data/lib/cryptum/ui/command.rb +314 -0
  65. data/lib/cryptum/ui/key_press_event.rb +33 -0
  66. data/lib/cryptum/ui/market_trend.rb +77 -0
  67. data/lib/cryptum/ui/order_execute_details.rb +297 -0
  68. data/lib/cryptum/ui/order_execution.rb +573 -0
  69. data/lib/cryptum/ui/order_plan.rb +512 -0
  70. data/lib/cryptum/ui/order_plan_details.rb +240 -0
  71. data/lib/cryptum/ui/order_timer.rb +136 -0
  72. data/lib/cryptum/ui/portfolio.rb +221 -0
  73. data/lib/cryptum/ui/signal_engine.rb +109 -0
  74. data/lib/cryptum/ui/terminal_window.rb +111 -0
  75. data/lib/cryptum/ui/ticker.rb +319 -0
  76. data/lib/cryptum/ui.rb +343 -0
  77. data/lib/cryptum/version.rb +5 -0
  78. data/lib/cryptum/web_sock/coinbase.rb +104 -0
  79. data/lib/cryptum/web_sock/event_machine.rb +276 -0
  80. data/lib/cryptum/web_sock.rb +16 -0
  81. data/lib/cryptum.rb +120 -0
  82. data/order_books/.gitkeep +0 -0
  83. data/reinstall_cryptum_gemset.sh +29 -0
  84. data/spec/lib/cryptum/api_spec.rb +10 -0
  85. data/spec/lib/cryptum/event_spec.rb +10 -0
  86. data/spec/lib/cryptum/log_spec.rb +10 -0
  87. data/spec/lib/cryptum/option_spec.rb +10 -0
  88. data/spec/lib/cryptum/order_book/generate_spec.rb +10 -0
  89. data/spec/lib/cryptum/order_book/market_trend_spec.rb +10 -0
  90. data/spec/lib/cryptum/order_book_spec.rb +10 -0
  91. data/spec/lib/cryptum/ui/command_spec.rb +10 -0
  92. data/spec/lib/cryptum/ui/ticker_spec.rb +10 -0
  93. data/spec/lib/cryptum/ui_spec.rb +10 -0
  94. data/spec/lib/cryptum/web_sock_spec.rb +10 -0
  95. data/spec/lib/cryptum_spec.rb +10 -0
  96. data/spec/spec_helper.rb +3 -0
  97. data/upgrade_Gemfile_gems.sh +20 -0
  98. data/upgrade_cryptum.sh +13 -0
  99. data/upgrade_gem.sh +4 -0
  100. data/upgrade_ruby.sh +45 -0
  101. metadata +112 -9
@@ -0,0 +1,181 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Cryptum
6
+ # This plugin is used for my own entertainment
7
+ module Matrix
8
+ # Supported Method Parameters::
9
+ # Cryptum::Matrix.generate(
10
+ # cols: cols
11
+ # )
12
+
13
+ public_class_method def self.generate(opts = {})
14
+ # Need to divide by two for wide characters
15
+ cols = opts[:cols].to_i
16
+
17
+ matrix_arr = [
18
+ 0x30a0.chr('UTF-8'),
19
+ 0x30a1.chr('UTF-8'),
20
+ 0x30a2.chr('UTF-8'),
21
+ 0x30a3.chr('UTF-8'),
22
+ 0x30a4.chr('UTF-8'),
23
+ 0x30a5.chr('UTF-8'),
24
+ 0x30a6.chr('UTF-8'),
25
+ 0x30a7.chr('UTF-8'),
26
+ 0x30a8.chr('UTF-8'),
27
+ 0x30a9.chr('UTF-8'),
28
+ 0x30aa.chr('UTF-8'),
29
+ 0x30ab.chr('UTF-8'),
30
+ 0x30ac.chr('UTF-8'),
31
+ 0x30ad.chr('UTF-8'),
32
+ 0x30ae.chr('UTF-8'),
33
+ 0x30af.chr('UTF-8'),
34
+ 0x30b0.chr('UTF-8'),
35
+ 0x30b1.chr('UTF-8'),
36
+ 0x30b2.chr('UTF-8'),
37
+ 0x30b3.chr('UTF-8'),
38
+ 0x30b4.chr('UTF-8'),
39
+ 0x30b5.chr('UTF-8'),
40
+ 0x30b6.chr('UTF-8'),
41
+ 0x30b7.chr('UTF-8'),
42
+ 0x30b8.chr('UTF-8'),
43
+ 0x30b9.chr('UTF-8'),
44
+ 0x30ba.chr('UTF-8'),
45
+ 0x30bb.chr('UTF-8'),
46
+ 0x30bc.chr('UTF-8'),
47
+ 0x30bd.chr('UTF-8'),
48
+ 0x30be.chr('UTF-8'),
49
+ 0x30bf.chr('UTF-8'),
50
+ 0x30c0.chr('UTF-8'),
51
+ 0x30c1.chr('UTF-8'),
52
+ 0x30c2.chr('UTF-8'),
53
+ 0x30c3.chr('UTF-8'),
54
+ 0x30c4.chr('UTF-8'),
55
+ 0x30c5.chr('UTF-8'),
56
+ 0x30c6.chr('UTF-8'),
57
+ 0x30c7.chr('UTF-8'),
58
+ 0x30c8.chr('UTF-8'),
59
+ 0x30c9.chr('UTF-8'),
60
+ 0x30ca.chr('UTF-8'),
61
+ 0x30cb.chr('UTF-8'),
62
+ 0x30cc.chr('UTF-8'),
63
+ 0x30cd.chr('UTF-8'),
64
+ 0x30ce.chr('UTF-8'),
65
+ 0x30cf.chr('UTF-8'),
66
+ 0x30d0.chr('UTF-8'),
67
+ 0x30d1.chr('UTF-8'),
68
+ 0x30d2.chr('UTF-8'),
69
+ 0x30d3.chr('UTF-8'),
70
+ 0x30d4.chr('UTF-8'),
71
+ 0x30d5.chr('UTF-8'),
72
+ 0x30d6.chr('UTF-8'),
73
+ 0x30d7.chr('UTF-8'),
74
+ 0x30d8.chr('UTF-8'),
75
+ 0x30d9.chr('UTF-8'),
76
+ 0x30da.chr('UTF-8'),
77
+ 0x30db.chr('UTF-8'),
78
+ 0x30dc.chr('UTF-8'),
79
+ 0x30dd.chr('UTF-8'),
80
+ 0x30de.chr('UTF-8'),
81
+ 0x30df.chr('UTF-8'),
82
+ 0x30e0.chr('UTF-8'),
83
+ 0x30e1.chr('UTF-8'),
84
+ 0x30e2.chr('UTF-8'),
85
+ 0x30e3.chr('UTF-8'),
86
+ 0x30e4.chr('UTF-8'),
87
+ 0x30e5.chr('UTF-8'),
88
+ 0x30e6.chr('UTF-8'),
89
+ 0x30e7.chr('UTF-8'),
90
+ 0x30e8.chr('UTF-8'),
91
+ 0x30e9.chr('UTF-8'),
92
+ 0x30ea.chr('UTF-8'),
93
+ 0x30eb.chr('UTF-8'),
94
+ 0x30ec.chr('UTF-8'),
95
+ 0x30ed.chr('UTF-8'),
96
+ 0x30ee.chr('UTF-8'),
97
+ 0x30ef.chr('UTF-8'),
98
+ 0x30f0.chr('UTF-8'),
99
+ 0x30f1.chr('UTF-8'),
100
+ 0x30f2.chr('UTF-8'),
101
+ 0x30f3.chr('UTF-8'),
102
+ 0x30f4.chr('UTF-8'),
103
+ 0x30f5.chr('UTF-8'),
104
+ 0x30f6.chr('UTF-8'),
105
+ 0x30f7.chr('UTF-8'),
106
+ 0x30f8.chr('UTF-8'),
107
+ 0x30f9.chr('UTF-8'),
108
+ 0x30fa.chr('UTF-8'),
109
+ 0x30fb.chr('UTF-8'),
110
+ 0x30fc.chr('UTF-8'),
111
+ 0x30fd.chr('UTF-8'),
112
+ 0x30fe.chr('UTF-8'),
113
+ '0 ',
114
+ '1 ',
115
+ '2 ',
116
+ '3 ',
117
+ '4 ',
118
+ '5 ',
119
+ '6 ',
120
+ '7 ',
121
+ '8 ',
122
+ '9 ',
123
+ 'A ',
124
+ 'c ',
125
+ 'R ',
126
+ 'y ',
127
+ 'P ',
128
+ 't ',
129
+ 'U ',
130
+ 'm ',
131
+ 'x ',
132
+ 'Z ',
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
+ last_index = matrix_arr.length - 1
162
+
163
+ matrix_row = ''
164
+ most_cols = cols - 1
165
+ most_cols.times.each do
166
+ matrix_row += "#{matrix_arr[Random.rand(0..last_index)]} "
167
+ end
168
+ matrix_row += matrix_arr[Random.rand(0..last_index)]
169
+ rescue StandardError => e
170
+ raise e
171
+ end
172
+
173
+ # Display Usage for this Module
174
+
175
+ public_class_method def self.help
176
+ puts "USAGE:
177
+ logger = #{self}.create()
178
+ "
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'rest-client'
5
+
6
+ module Cryptum
7
+ # This plugin is used for interacting w/ OpenAI's REST API
8
+ # This is based on the following OpenAI API Specification:
9
+ # https://api.openai.com/v1
10
+ module OpenAI
11
+ # Supported Method Parameters::
12
+ # open_ai_rest_call(
13
+ # env: 'required - env object containing OpenAI Bearer token',
14
+ # option_choice = 'required - option_choice object in case proxy is configured',
15
+ # http_method: 'optional HTTP method (defaults to GET)
16
+ # rest_call: 'required rest call to make per the schema',
17
+ # params: 'optional params passed in the URI or HTTP Headers',
18
+ # http_body: 'optional HTTP body sent in HTTP methods that support it e.g. POST'
19
+ # )
20
+
21
+ private_class_method def self.open_ai_rest_call(opts = {})
22
+ env = opts[:env]
23
+ option_choice = opts[:option_choice]
24
+ http_method = if opts[:http_method].nil?
25
+ :GET
26
+ else
27
+ opts[:http_method].to_s.upcase.scrub.strip.chomp.to_sym
28
+ end
29
+ rest_call = opts[:rest_call].to_s.scrub
30
+ params = opts[:params]
31
+ http_body = opts[:http_body].to_s.scrub
32
+ base_open_ai_api_uri = 'https://api.openai.com/v1'
33
+ bearer_token = env[:open_ai_bearer_token]
34
+
35
+ if option_choice.proxy
36
+ rest_client = RestClient
37
+ rest_client.proxy = option_choice.proxy
38
+ rest_client_request = rest_client::Request
39
+ else
40
+ rest_client_request = RestClient::Request
41
+ end
42
+
43
+ case http_method
44
+ when :GET
45
+ response = rest_client_request.execute(
46
+ method: :GET,
47
+ url: "#{base_open_ai_api_uri}/#{rest_call}",
48
+ headers: {
49
+ content_type: 'application/json; charset=UTF-8',
50
+ authorization: "Bearer #{bearer_token}",
51
+ params: params
52
+ },
53
+ verify_ssl: false
54
+ )
55
+
56
+ when :POST
57
+ response = rest_client_request.execute(
58
+ method: :POST,
59
+ url: "#{base_open_ai_api_uri}/#{rest_call}",
60
+ headers: {
61
+ content_type: 'application/json; charset=UTF-8',
62
+ authorization: "Bearer #{bearer_token}"
63
+ },
64
+ payload: http_body,
65
+ verify_ssl: false
66
+ )
67
+
68
+ else
69
+ raise @@logger.error("Unsupported HTTP Method #{http_method} for #{self} Plugin")
70
+ end
71
+ response
72
+ rescue RestClient::ExceptionWithResponse => e
73
+ File.open('/tmp/cryptum-errors.txt', 'a') do |f|
74
+ f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z')
75
+ f.puts "Module: #{self}"
76
+ f.puts "URL: #{api_endpoint}#{api_call}"
77
+ f.puts "PARAMS: #{params.inspect}"
78
+ f.puts "HTTP POST BODY: #{http_body.inspect}" if http_body != ''
79
+ f.puts "#{e}\n#{e.response}\n\n\n"
80
+ end
81
+ rescue StandardError => e
82
+ raise e
83
+ end
84
+
85
+ # Supported Method Parameters::
86
+ # models = PWN::Plugins::OpenAI.get_models(
87
+ # env: 'required - env object containing OpenAI Bearer token',
88
+ # option_choice = 'required - option_choice object in case proxy is configured',
89
+ # )
90
+
91
+ public_class_method def self.get_models(opts = {})
92
+ env = opts[:env]
93
+ option_choice = opts[:option_choice]
94
+
95
+ response = open_ai_rest_call(
96
+ env: env,
97
+ option_choice: option_choice,
98
+ http_method: :post,
99
+ rest_call: 'models'
100
+ )
101
+
102
+ JSON.parse(response, symbolize_names: true)
103
+ rescue StandardError => e
104
+ raise e
105
+ end
106
+
107
+ # Supported Method Parameters::
108
+ # response = PWN::Plugins::OpenAI.chat_gpt(
109
+ # env: 'required - env object containing OpenAI Bearer token',
110
+ # option_choice = 'required - option_choice object in case proxy is configured',
111
+ # request: 'required - message to ChatGPT'
112
+ # )
113
+
114
+ public_class_method def self.chat_gpt(opts = {})
115
+ env = opts[:env]
116
+ option_choice = opts[:option_choice]
117
+ request = opts[:request]
118
+
119
+ http_body = {
120
+ model: 'text-davinci-003',
121
+ prompt: request,
122
+ temperature: 0,
123
+ max_tokens: 1024
124
+ }
125
+
126
+ response = open_ai_rest_call(
127
+ env: env,
128
+ option_choice: option_choice,
129
+ http_method: :post,
130
+ rest_call: 'completions',
131
+ http_body: http_body.to_json
132
+ )
133
+
134
+ JSON.parse(response, symbolize_names: true)
135
+ rescue StandardError => e
136
+ raise e
137
+ end
138
+
139
+ # Display Usage for this Module
140
+
141
+ public_class_method def self.help
142
+ puts "USAGE:
143
+ models = PWN::Plugins::OpenAI.get_models(
144
+ env: 'required - env object containing OpenAI Bearer token',
145
+ option_choice = 'required - option_choice object in case proxy is configured',
146
+ )
147
+
148
+ response = #{self}.chat_gpt(
149
+ env: 'required - env object containing OpenAI Bearer token',
150
+ option_choice = 'required - option_choice object in case proxy is configured',
151
+ request: 'required - message to ChatGPT'
152
+ )
153
+ "
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptum
4
+ # This plugin is used to Cancel Open Limit Orders
5
+ module Option
6
+ # Keep Last Event History When Terminal Resize Occuers
7
+ class Choice
8
+ attr_accessor :autotrade,
9
+ :driver_name,
10
+ :list_products,
11
+ :proxy,
12
+ :reset_session_countdown,
13
+ :session_root,
14
+ :sandbox,
15
+ :symbol,
16
+ :market_trend_reset,
17
+ :market_trend_reset_label
18
+
19
+ rescue Interrupt
20
+ # Exit Gracefully if CTRL+C is Pressed During Session
21
+ Cryptum.exit_gracefully(which_self: self)
22
+ rescue StandardError => e
23
+ # Produce a Stacktrace for anything else
24
+ Curses.close_screen
25
+ raise e
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require 'yaml'
5
+
6
+ module Cryptum
7
+ # Cryptum::UI Module used for Presenting the
8
+ # Cryptum Curses Interface
9
+ module Option
10
+ require 'cryptum/option/choice'
11
+
12
+ # Options for cryptum Driver
13
+ public_class_method def self.parser(opts = {})
14
+ option_choice = Option::Choice.new
15
+ option_choice.driver_name = opts[:driver_name]
16
+
17
+ OptionParser.new do |options|
18
+ options.banner = "USAGE: #{option_choice.driver_name} [opts]"
19
+
20
+ options.on(
21
+ '-sSYMBOL',
22
+ '--symbol=SYMBOL',
23
+ '<Required - Crypto Symbol.(e.g. btc-usd, eth-usd, etc.)'
24
+ ) { |s| option_choice.symbol = s.to_s.gsub('-', '_').downcase.to_sym }
25
+
26
+ options.on(
27
+ '-A',
28
+ '--[no-]autotrade',
29
+ '<Optional - Automatically Buy and Sell Crypto>'
30
+ ) { |a| option_choice.autotrade = a }
31
+
32
+ options.on(
33
+ '-l',
34
+ '--[no-]list-products',
35
+ '<Optional - List Supported Crypto Currency Products and Exit>'
36
+ ) { |l| option_choice.list_products = l }
37
+
38
+ options.on(
39
+ '-pPROXY',
40
+ '--proxy=PROXY',
41
+ '<Optional - HTTP Proxy e.g. "http://127.0.0.1:8080">'
42
+ ) { |p| option_choice.proxy = p }
43
+
44
+ options.on(
45
+ '-R',
46
+ '--[no-]reset-session-countdown',
47
+ '<Optional - Reset Timers / Market Trend Stats at Session Init (Defaults to false)>'
48
+ ) { |t| option_choice.reset_session_countdown = t }
49
+
50
+ options.on(
51
+ '-rPATH',
52
+ '--session-root=PATH',
53
+ '<Optional - Directory with etc && order_books (Defaults to ~/cryptum)>'
54
+ ) { |r| option_choice.session_root = r }
55
+
56
+ options.on(
57
+ '-S',
58
+ '--[no-]sandbox',
59
+ '<Optional - Use Coinbase Sandbox Environment for Testing Ideas>'
60
+ ) { |n| option_choice.sandbox = n }
61
+
62
+ options.on(
63
+ '-tSECONDS',
64
+ '--time-between-market-trend-reset=SECONDS',
65
+ '<Optional - Seconds Between Market Trend Reset (Default 60 i.e. 1 hour)>'
66
+ ) { |t| option_choice.market_trend_reset = t.to_i }
67
+ end.parse!
68
+
69
+ input_validation(option_choice: option_choice)
70
+
71
+ option_choice
72
+ rescue OptionParser::InvalidOption => e
73
+ # Print Usage if unsupported flags are passed
74
+ puts "ERROR: #{e.message}\n\n"
75
+ puts `#{option_choice.driver_name} --help`
76
+ exit 1
77
+ rescue StandardError => e
78
+ raise e
79
+ end
80
+
81
+ # Validate Options for cryptum Driver
82
+ public_class_method def self.input_validation(opts = {})
83
+ option_choice = opts[:option_choice]
84
+
85
+ # Conditions to display cryptum usage
86
+ if option_choice.symbol.nil? && option_choice.list_products.nil?
87
+ usage = true
88
+ reason = :symbol
89
+ end
90
+
91
+ option_choice.session_root = "#{Dir.home}/cryptum" if option_choice.session_root.nil?
92
+
93
+ unless Dir.exist?(option_choice.session_root)
94
+ usage = true
95
+ reason = :session_root
96
+ end
97
+
98
+ option_choice.market_trend_reset = 86_400 if option_choice.market_trend_reset.to_i.zero?
99
+ unless option_choice.market_trend_reset.to_i >= 60 &&
100
+ option_choice.market_trend_reset <= 604_800
101
+ usage = true
102
+ reason = :market_trend_reset
103
+ end
104
+
105
+ case option_choice.market_trend_reset
106
+ when 604_800
107
+ option_choice.market_trend_reset_label = '1W'
108
+ when 86_400
109
+ option_choice.market_trend_reset_label = '1D'
110
+ when 14_400
111
+ option_choice.market_trend_reset_label = '4h'
112
+ when 10_800
113
+ option_choice.market_trend_reset_label = '3h'
114
+ when 7_200
115
+ option_choice.market_trend_reset_label = '2h'
116
+ when 3_600
117
+ option_choice.market_trend_reset_label = '1h'
118
+ when 2_700
119
+ option_choice.market_trend_reset_label = '45m'
120
+ when 1_800
121
+ option_choice.market_trend_reset_label = '30m'
122
+ when 900
123
+ option_choice.market_trend_reset_label = '15m'
124
+ when 300
125
+ option_choice.market_trend_reset_label = '5m'
126
+ when 180
127
+ option_choice.market_trend_reset_label = '3m'
128
+ when 60
129
+ option_choice.market_trend_reset_label = '1m'
130
+ else
131
+ usage = true
132
+ reason = :market_trend_reset
133
+ end
134
+
135
+ if usage
136
+ case reason
137
+ when :symbol
138
+ puts "ERROR: --symbol Flag is Required.\n\n"
139
+ when :session_root
140
+ puts "ERROR: #{option_choice.session_root} does not exist.\n\n"
141
+ when :market_trend_reset
142
+ puts "ERROR: #{option_choice.market_trend_reset} - Possible values are: 604_800 || 86_400 || 14_400 || 10_800 || 7_200 || 3_600 || 2_700 || 1_800 || 900 || 300 || 180 || 60\n\n"
143
+ end
144
+
145
+ puts `#{option_choice.driver_name} --help`
146
+ exit 1
147
+ end
148
+ rescue StandardError => e
149
+ raise e
150
+ end
151
+
152
+ # List Supported Cryptum Products and Exit
153
+ public_class_method def self.list_products_and_exit(opts = {})
154
+ option_choice = opts[:option_choice]
155
+ env = opts[:env]
156
+
157
+ puts "\n#{option_choice.driver_name} Supports the Following Products:"
158
+ products = Cryptum::API.get_products(
159
+ option_choice: option_choice,
160
+ env: env
161
+ )
162
+
163
+ products.map do |product|
164
+ puts product[:id].downcase
165
+ end
166
+
167
+ exit 0
168
+ rescue StandardError => e
169
+ raise e
170
+ end
171
+
172
+ # Initialize Cryptum Session Environment
173
+ public_class_method def self.get_env(opts = {})
174
+ option_choice = opts[:option_choice]
175
+
176
+ yaml_conf_file = "#{option_choice.session_root}/etc/coinbase_pro.yaml"
177
+ yaml_conf = YAML.load_file(
178
+ yaml_conf_file,
179
+ symbolize_names: true
180
+ )
181
+
182
+ env = yaml_conf[:prod]
183
+ env[:env] = :prod
184
+ env = yaml_conf[:sandbox] if option_choice.sandbox
185
+ env[:env] = :sandbox if option_choice.sandbox
186
+
187
+ open_ai_yaml_conf_file = "#{option_choice.session_root}/etc/open_ai.yaml"
188
+ if File.exist?(open_ai_yaml_conf_file)
189
+ open_ai_yaml_conf = YAML.load_file(
190
+ open_ai_yaml_conf_file,
191
+ symbolize_names: true
192
+ )
193
+ env[:open_ai_bearer_token] = open_ai_yaml_conf[:bearer_token]
194
+ end
195
+
196
+ env
197
+ rescue StandardError => e
198
+ raise e
199
+ end
200
+
201
+ # Display a List of Every UI Module
202
+ public_class_method def self.help
203
+ constants.sort
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'io/console'
4
+ require 'json'
5
+ require 'logger'
6
+ require 'tty-spinner'
7
+
8
+ module Cryptum
9
+ # This module is used to define the Order Book Data Structure
10
+ module OrderBook
11
+ module Generate
12
+ # Supported Method Parameters::
13
+ # Cryptum::OrderBook.generate(
14
+ # symbol: 'required - target symbol (e.g. btc-usd)',
15
+ # this_product: 'required - this_product',
16
+ # )
17
+
18
+ public_class_method def self.new_order_book(opts = {})
19
+ start_time = opts[:start_time]
20
+ option_choice = opts[:option_choice]
21
+ env = opts[:env]
22
+
23
+ session_root = option_choice.session_root
24
+ symbol = option_choice.symbol
25
+ order_book_file = "#{session_root}/order_books/#{symbol}.ORDER_BOOK.json"
26
+
27
+ # Only need to retrieve a product list once / session.
28
+ products = Cryptum::API.get_products(
29
+ option_choice: option_choice,
30
+ env: env
31
+ )
32
+ this_product_arr = products.select do |product|
33
+ product if product[:id] == option_choice.symbol.to_s.gsub('_', '-').upcase
34
+ end
35
+ this_product = this_product_arr.last
36
+
37
+ order_book = {
38
+ path: order_book_file,
39
+ symbol: symbol,
40
+ open_24h: 0.00,
41
+ high_24h: 0.00,
42
+ low_24h: 0.00,
43
+ volume_24h: 0.00,
44
+ ticker_price: 0.00,
45
+ ticker_price_second_to_last: 0.00,
46
+ ticker_price_third_to_last: 0.00,
47
+ sequence: -1,
48
+ this_product: this_product,
49
+ portfolio: [],
50
+ fiat_portfolio: [],
51
+ fees: [],
52
+ order_plan: [],
53
+ last_trend_reset: Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z'),
54
+ last_order_exec: Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z'),
55
+ market_trend: {
56
+ buy: 0,
57
+ buy_start: '--',
58
+ buy_end: '--',
59
+ sell: 0,
60
+ sell_start: '--',
61
+ sell_end: '--'
62
+ },
63
+ order_history: [],
64
+ order_history_meta: []
65
+ }
66
+
67
+ # Order History Retention ---------------------------------------#
68
+ # Instantiate Event History attr_accessible
69
+ # Object to Keep Track of Everything as Events
70
+ # are Parsed.
71
+ event_history = Cryptum::Event::History.new(
72
+ option_choice: option_choice,
73
+ start_time: start_time,
74
+ order_book: order_book
75
+ )
76
+
77
+ if File.exist?(order_book_file)
78
+ order_book = JSON.parse(
79
+ File.read(order_book_file),
80
+ symbolize_names: true
81
+ )
82
+ end
83
+
84
+ event_history.order_book = order_book
85
+ if option_choice.reset_session_countdown
86
+ event_history.order_book[:order_plan] = []
87
+ event_history.order_book[:market_trend][:buy] = 0
88
+ event_history.order_book[:market_trend][:sell] = 0
89
+ event_history.order_book[:last_trend_reset] = Time.now.strftime(
90
+ '%Y-%m-%d %H:%M:%S.%N%z'
91
+ )
92
+ event_history.order_book[:last_order_exec] = Time.now.strftime(
93
+ '%Y-%m-%d %H:%M:%S.%N%z'
94
+ )
95
+ end
96
+
97
+ event_history
98
+ rescue StandardError => e
99
+ raise e
100
+ end
101
+
102
+ # Display Usage for this Module
103
+
104
+ public_class_method def self.help
105
+ puts "USAGE:
106
+ order_book = #{self}.new_order_book(
107
+ symbol: 'required - target symbol (e.g. btc-usd)',
108
+ this_product: 'required - this_product'
109
+ )
110
+ "
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptum
4
+ # This plugin is used to calculate Exponential Moving Average
5
+ # Price of a Symbol over time.
6
+
7
+ module OrderBook
8
+ class Indicator
9
+ attr_accessor :market_trend,
10
+ :profit_margin,
11
+ :action_signal,
12
+ :last_action_signal
13
+ end
14
+ end
15
+ end