Class: Rebalance::Rebalancer
- Inherits:
-
Object
- Object
- Rebalance::Rebalancer
- Defined in:
- lib/rebalance/rebalancer.rb
Instance Attribute Summary collapse
-
#acceptable_asset_class_delta ⇒ Object
Returns the value of attribute acceptable_asset_class_delta.
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#rebalanced_share_difference ⇒ Object
Returns the value of attribute rebalanced_share_difference.
-
#rebalanced_shares ⇒ Object
Returns the value of attribute rebalanced_shares.
-
#rebalanced_value_difference ⇒ Object
Returns the value of attribute rebalanced_value_difference.
-
#rebalanced_values ⇒ Object
Returns the value of attribute rebalanced_values.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #calculate_rebalanced_asset_class_values ⇒ Object
- #funds_by_asset_class ⇒ Object
-
#initialize(target, *accounts) ⇒ Rebalancer
constructor
A new instance of Rebalancer.
- #initialize_result_values ⇒ Object
- #print_results ⇒ Object
- #rebalance ⇒ Object
-
#results ⇒ Object
Print a pretty table of our results.
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_delta ⇒ Object
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 |
#accounts ⇒ Object
Returns the value of attribute accounts.
5 6 7 |
# File 'lib/rebalance/rebalancer.rb', line 5 def accounts @accounts end |
#rebalanced_share_difference ⇒ Object
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_shares ⇒ Object
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_difference ⇒ Object
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_values ⇒ Object
Returns the value of attribute rebalanced_values.
5 6 7 |
# File 'lib/rebalance/rebalancer.rb', line 5 def rebalanced_values @rebalanced_values end |
#target ⇒ Object
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_values ⇒ Object
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 |account| account.funds.each do |symbol, fund| symbol_asset_class_hash[symbol] = fund.asset_class end end values = {} if @accounts.size > 1 @rebalanced_values.each do |account, 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_class ⇒ Object
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 |account| account.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_values ⇒ Object
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 |account| @rebalanced_shares[account.name] = {} @rebalanced_values[account.name] = {} @rebalanced_share_difference[account.name] = {} @rebalanced_value_difference[account.name] = {} end end end |
#print_results ⇒ Object
110 111 112 |
# File 'lib/rebalance/rebalancer.rb', line 110 def print_results p results end |
#rebalance ⇒ Object
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 single_account_rebalance elsif @accounts.size > 1 multiple_account_rebalance end end |
#results ⇒ Object
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 |account, 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 = [account, symbol, asset_class, format_currency(price), format_currency(amount_to_buy), format_currency(amount_to_sell)] data << data_array end end data end |