Class: Rebalance::Rebalancer

Inherits:
Object
  • Object
show all
Defined in:
lib/rebalance/rebalancer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, *accounts) ⇒ Rebalancer

Returns a new instance of Rebalancer.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rebalance/rebalancer.rb', line 13

def initialize(target, *accounts)
  @target = target
  # make sure we add empty funds where appropriate before we start rebalancing
  # in order to allow all accounts/funds to balance properly
  @accounts = accounts

  @acceptable_asset_class_delta = 1.0

  initialize_result_values
end

Instance Attribute Details

#acceptable_asset_class_deltaObject

Returns the value of attribute acceptable_asset_class_delta.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def acceptable_asset_class_delta
  @acceptable_asset_class_delta
end

#accountsObject

Returns the value of attribute accounts.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def accounts
  @accounts
end

#rebalanced_share_differenceObject

Returns the value of attribute rebalanced_share_difference.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def rebalanced_share_difference
  @rebalanced_share_difference
end

#rebalanced_sharesObject

Returns the value of attribute rebalanced_shares.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def rebalanced_shares
  @rebalanced_shares
end

#rebalanced_value_differenceObject

Returns the value of attribute rebalanced_value_difference.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def rebalanced_value_difference
  @rebalanced_value_difference
end

#rebalanced_valuesObject

Returns the value of attribute rebalanced_values.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def rebalanced_values
  @rebalanced_values
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/rebalance/rebalancer.rb', line 5

def target
  @target
end

Instance Method Details

#calculate_rebalanced_asset_class_valuesObject



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
# File 'lib/rebalance/rebalancer.rb', line 50

def calculate_rebalanced_asset_class_values
  # First, create a hash of symbols and their asset classes
  symbol_asset_class_hash = {}
  @accounts.each do ||
    .funds.each do |symbol, fund|
      symbol_asset_class_hash[symbol] = fund.asset_class
    end
  end

  values = {}
  if @accounts.size > 1
    @rebalanced_values.each do |, symbol_value_hash|
      symbol_value_hash.each do |symbol, value|
        asset_class = symbol_asset_class_hash[symbol]
        values[asset_class] = 0 if values[asset_class].nil?
        values[asset_class] += value
      end
    end
  else
    @rebalanced_values.each do |symbol, value|
      asset_class = symbol_asset_class_hash[symbol]
      values[asset_class] = 0 if values[asset_class].nil?
      values[asset_class] += value
    end
  end
  values
end

#funds_by_asset_classObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/rebalance/rebalancer.rb', line 78

def funds_by_asset_class
  asset_class_hash = {}
  @accounts.each do ||
    .funds.each do |symbol, fund|
      asset_class_hash[fund.asset_class] = [] if asset_class_hash[fund.asset_class].nil?
      asset_class_hash[fund.asset_class] << fund if !asset_class_hash[fund.asset_class].include?(fund)
    end
  end
  asset_class_hash
end

#initialize_result_valuesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rebalance/rebalancer.rb', line 24

def initialize_result_values
  @rebalanced_shares = {}
  @rebalanced_values = {}
  @rebalanced_share_difference = {}
  @rebalanced_value_difference = {}

  if @accounts.size > 1
    @accounts.each do ||
      @rebalanced_shares[.name] = {}
      @rebalanced_values[.name] = {}
      @rebalanced_share_difference[.name] = {}
      @rebalanced_value_difference[.name] = {}
    end
  end
end


110
111
112
# File 'lib/rebalance/rebalancer.rb', line 110

def print_results
  p results
end

#rebalanceObject



40
41
42
43
44
45
46
47
48
# File 'lib/rebalance/rebalancer.rb', line 40

def rebalance
  initialize_result_values

  if @accounts.size == 1
    
  elsif @accounts.size > 1
    
  end
end

#resultsObject

Print a pretty table of our results



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rebalance/rebalancer.rb', line 90

def results
  data = Table('Account','Fund','Asset Class', 'Price','Amount To Buy','Amount To Sell')

  @rebalanced_value_difference.each do |, funds|
    funds.each do |symbol, val_diff|
      price = fund_hash[symbol].price
      asset_class = fund_hash[symbol].asset_class
      amount_to_buy = 0
      amount_to_buy = val_diff.abs if val_diff > 0
      amount_to_sell = 0
      amount_to_sell = val_diff.abs if val_diff < 0

      data_array = [, symbol, asset_class, format_currency(price), format_currency(amount_to_buy), format_currency(amount_to_sell)]
      data << data_array
    end
  end

  data
end