Module: Coinbot::OrderBook::StuckInPosition

Defined in:
lib/coinbot/order_book/stuck_in_position.rb

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



139
140
141
142
143
# File 'lib/coinbot/order_book/stuck_in_position.rb', line 139

public_class_method def self.help
  puts "USAGE:
    #{self}.status()
  "
end

.status(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::OrderBook::StuckInPosition.status( )



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
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
# File 'lib/coinbot/order_book/stuck_in_position.rb', line 12

public_class_method def self.status(opts = {})
  order_book = opts[:order_book]
  indicator_status = opts[:indicator_status]
  invested = opts[:invested]
  bot_conf = opts[:bot_conf]

  this_product = order_book[:this_product]
  # base_min_size = this_product[:base_min_size]
  # base_max_size = this_product[:base_max_size]
  # min_market_funds = this_product[:min_market_funds]
  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

  candles = order_book[:candles]

  indicator_hash = {}
  indicator_hash[:invested] = invested
  indicator_hash[:status] = 'SiP'

  third_last_color = candles.last[:last_sip_color]
  candles.last[:third_last_sip_color] = third_last_color

  last_color = candles.last[:sip_color]
  candles.last[:last_sip_color] = last_color

  if invested
    portfolio = order_book[:portfolio]
    order_history = order_book[:order_history]
    symbol = order_book[:symbol].to_s.upcase
    crypto_currency = symbol.split('_').first
    symbol_portfolio = portfolio.select do |this_portfolio|
      this_portfolio if this_portfolio[:currency] == crypto_currency
    end
    symbol_balance = symbol_portfolio.first[:balance].to_f

    # This block derives the cost incurred for the
    # current crypto balance based upon previous orders
    # if base_min_size.to_i == 1
    #   order_size = 0
    # else
    order_size = 0.0
    # end

    total_invested_in_symbol = 0.00
    order_history_done = order_history.select { |order| order[:status] == 'done' }

    # 1. UNCOMMENT TO DEBUG:
    # File.open('/tmp/sip.debug', 'w') do |debug|
    remaining_symbol_balance = symbol_balance
    order_history_done.each_with_index do |order, _index|
      # this_iteration = index + 1
      this_order_side = order[:side]
      this_order_price = order[:price].to_f
      this_order_size = order[:filled_size].to_f
      # this_executed_value = order[:executed_value].to_f
      this_executed_value = this_order_price * this_order_size
      # this_order_status = order[:status]

      # next unless remaining_symbol_balance >= base_min_size.to_f
      next unless remaining_symbol_balance.positive?

      case this_order_side.to_sym
      when :buy
        total_invested_in_symbol += format("%0.#{fiat_smallest_decimal}f", this_executed_value).to_f
        # if base_min_size.to_i == 1
        #   order_size += this_order_size.to_i
        # else
        order_size += format("%0.#{crypto_smallest_decimal}f", this_order_size).to_f
        # end
      when :sell
        total_invested_in_symbol -= format("%0.#{fiat_smallest_decimal}f", this_executed_value).to_f
        # if base_min_size.to_i == 1
        #   order_size -= this_order_size.to_i
        # else
        order_size -= format("%0.#{crypto_smallest_decimal}f", this_order_size).to_f
        # end
      end
      remaining_symbol_balance = symbol_balance - order_size
      # 2. UNCOMMENT TO DEBUG:
      # debug.puts "Iteration ##{this_iteration}:"
      # debug.puts "\tSymbol Balance - Order Size = Remaining Symbol Balance"
      # debug.puts "\t#{symbol_balance} - #{order_size} = #{remaining_symbol_balance}"
      # debug.puts "\tTotal Invested in Symbol => $#{total_invested_in_symbol}\n\n\n"
    end
    # 3. UNCOMMENT TO DEBUG:
    # end

    last_ticker_price = order_book[:ticker_price].to_f
    # What is my crypto _BALANCE_ (not ticker price) CURRENTLY worth?
    current_symbol_value = symbol_balance * last_ticker_price
    # Stuck in Position unless profit margin goals are met.
    tpm_cast_as_decimal = format(
      '%0.7f',
      bot_conf[:target_profit_margin_percent] * 0.01
    ).to_f

    # This is the invested _BALANCE_ (not ticker price) targeted
    # to be at or above in order to turn this indicator green.
    total_invested_w_tpm = total_invested_in_symbol + (total_invested_in_symbol * tpm_cast_as_decimal)

    # This is the targeted ticker price.
    target_symbol_price = total_invested_w_tpm / symbol_balance

    # This will earn precisely greater than or equal
    # to our target profit margin (TPM)
    indicator_hash[:color] = :red
    indicator_hash[:color] = :green if current_symbol_value >= total_invested_w_tpm &&
                                       total_invested_w_tpm.positive?

    indicator_hash[:current_symbol_value] = current_symbol_value
    indicator_hash[:total_invested_in_symbol_w_target_pm] = total_invested_w_tpm
    indicator_hash[:target_symbol_price] = target_symbol_price

  else
    indicator_hash[:color] = :white
  end

  candles.last[:sip_color] = indicator_hash[:color]
  indicator_status.stuck_in_position = indicator_hash
rescue StandardError => e
  raise e
end