Module: Cryptum::BotConf

Defined in:
lib/cryptum/bot_conf.rb

Overview

This plugin is used to read and update bot conf files.

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



191
192
193
194
195
# File 'lib/cryptum/bot_conf.rb', line 191

public_class_method def self.help
  puts "USAGE:
    logger = #{self}.create()
  "
end

.read(opts = {}) ⇒ Object

Deserialize Cryptum Bot Conf



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
# File 'lib/cryptum/bot_conf.rb', line 9

public_class_method def self.read(opts = {})
  option_choice = opts[:option_choice]
  event_history = opts[:event_history]

  session_root = option_choice.session_root
  symbol = option_choice.symbol
  bot_conf_file = "#{session_root}/etc/bot_confs/#{symbol}_bot_conf.yaml"
  unless File.exist?(bot_conf_file)
    FileUtils.cp(
      "#{session_root}/etc/bot_confs/BOT_CONF.TEMPLATE",
      bot_conf_file
    )
  end

  bot_conf = YAML.load_file(
    bot_conf_file,
    symbolize_names: true
  )

  ai_enabled = bot_conf[:artifical_intelligence]
  if ai_enabled && event_history
    bot_conf = Cryptum::BotConf.recalculate_tpm(
      option_choice: option_choice,
      event_history: event_history,
      bot_conf: bot_conf
    )
  end

  bot_conf
rescue Errno::ENOENT, NoMethodError => e
  File.open('/tmp/cryptum-errors.txt', 'a') do |f|
    f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z')
    f.puts "Module: #{self}"
    f.puts "#{e}\n\n\n"
  end

  retry
rescue Interrupt
  # Exit Gracefully if CTRL+C is Pressed During Session
  Cryptum.exit_gracefully(which_self: self)
rescue StandardError => e
  # Produce a Stacktrace for anything else
  Curses.close_screen
  raise e
end

.recalculate_tpm(opts = {}) ⇒ Object

SAUCE 1 Calculate Target Profit Margin Percent Based Upon Observed Margins of Change in the Past 24hrs.



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cryptum/bot_conf.rb', line 58

public_class_method def self.recalculate_tpm(opts = {})
  option_choice = opts[:option_choice]
  event_history = opts[:event_history]
  bot_conf = opts[:bot_conf]

  # BE EXTREMELY CAREFUL CHANGING THIS VALUE AS IT DICTATES
  # THE TARGET PRICE AND SUBSEQUENT TIME IT TAKES FOR AN OPEN
  # SELL ORDER TO BE TRIGGERED!!! SHOULD NEVER BE > 1
  default_net_tpm = 1.0
  maker_rate = 0.4
  taker_rate = 0.6

  gross_tpm = bot_conf[:target_profit_margin_percent].to_f

  # Refactor TPM to be 1.0 > than fee tier,
  # particularly as fee tier goes up or down
  fees = event_history.order_book[:fees]
  maker_rate = fees[:maker_fee_rate].to_f unless fees.empty?
  # maker_fee = format('%0.2f', maker_rate * 100)

  taker_rate = fees[:taker_fee_rate].to_f unless fees.empty?
  # taker_fee = format('%0.2f', taker_rate * 100)

  # Set default_net_tpm if AI is true in bot_conf.
  low_24h = event_history.order_book[:low_24h].to_f
  high_24h = event_history.order_book[:high_24h].to_f

  case option_choice.market_trend_reset
  when 604_800
    # 1W Chart
    ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) * 7
  when 86_400
    # 1D Chart
    ai_net_tpm = (1 - (low_24h / high_24h)) * 100
  when 14_400
    # 4H Chart
    ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 6
  when 10_800
    # 3H Chart
    ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 8
  when 7_200
    # 2H Chart
    ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 12
  when 3_600
    # 1H Chart
    ai_net_tpm = ((1 - (low_24h / high_24h)) * 100) / 24
  when 2_700
    # 45m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.75
  when 1_800
    # 30m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.5
  when 900
    # 15m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.25
  when 300
    # 5m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.083
  when 180
    # 3m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.05
  when 60
    # 1m Chart
    ai_net_tpm = (((1 - (low_24h / high_24h)) * 100) / 24) * 0.017
  end

  default_net_tpm = ai_net_tpm if ai_net_tpm > default_net_tpm

  min_gross_tpm = format(
    '%0.2f',
    (maker_rate.to_f + taker_rate.to_f) + default_net_tpm
  )

  if min_gross_tpm != gross_tpm.to_s
    bot_conf[:target_profit_margin_percent] = min_gross_tpm.to_f
    Cryptum::BotConf.update(
      option_choice: option_choice,
      bot_conf: bot_conf,
      key: :target_profit_margin_percent,
      value: min_gross_tpm.to_f
    )
  end

  bot_conf
rescue Errno::ENOENT, NoMethodError => e
  File.open('/tmp/cryptum-errors.txt', 'a') do |f|
    f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z')
    f.puts "Module: #{self}"
    f.puts "#{e}\n\n\n"
  end

  retry
rescue Interrupt
  # Exit Gracefully if CTRL+C is Pressed During Session
  Cryptum.exit_gracefully(which_self: self)
rescue StandardError => e
  # Produce a Stacktrace for anything else
  Curses.close_screen
  raise e
end

.update(opts = {}) ⇒ Object

Update Key/Value Pair in Bot Conf and Serialize to YAML File



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
# File 'lib/cryptum/bot_conf.rb', line 160

public_class_method def self.update(opts = {})
  option_choice = opts[:option_choice]
  bot_conf = opts[:bot_conf]
  key = opts[:key].to_s.to_sym
  value = opts[:value]

  session_root = option_choice.session_root
  symbol = option_choice.symbol
  bot_conf_file = "#{session_root}/etc/bot_confs/#{symbol}_bot_conf.yaml"

  bot_conf[key] = value
  File.write(bot_conf_file, bot_conf.to_yaml)
rescue Errno::ENOENT, NoMethodError => e
  File.open('/tmp/cryptum-errors.txt', 'a') do |f|
    f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N %z')
    f.puts "Module: #{self}"
    f.puts "#{e}\n\n\n"
  end

  retry
rescue Interrupt
  # Exit Gracefully if CTRL+C is Pressed During Session
  Cryptum.exit_gracefully(which_self: self)
rescue StandardError => e
  # Produce a Stacktrace for anything else
  Curses.close_screen
  raise e
end